On Thu, Mar 25, 2010 at 1:54 PM, Pry, Jeffrey <jeffrey....@sig.com> wrote:
> sub displayPage($) {
>
>            my($server) = shift;
>            print $server;
> }
>

Hi,

I'd repeat the advice about staying away from prototypes, i.e. the
'($)' business after your subroutine name. Perl is very good at
figuring out what you mean when you pass something to a subroutine.
This is how I would write it:

#!/usr/bin/env perl
use strict;
use warnings;

display_page('server','name','secret');

sub display_page {
    my ($host, $user, $pass) = @_;
    printf "proto://%s:%...@%s\n", $user,$pass,$host;
}

If you'll recall, @a is an array and $a[0] is the first element of
that array. The arguments array @_ that Perl uses to store the things
you are passing to the subroutine works the same way, so $_[0] is the
first element of that array. Perl's array manipulators default to @_
if you don't tell it otherwise, so $host = shift is the same as $host
= shift @_, taking the first element of @_ and storing it in
$host--it's also idiomatic Perl.

I think the way I've written it above is a bit neater and easier to
read than some other options for a list of this length, but it's
really up to you.

By the way, calling subroutines with ampersand '&' is no longer
recommended, mainly because it clutters up your code. And if you *do*
actually want to use prototypes, then '&' cannot be used.

-d.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to