Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: my $x or my ($x)

by Melly (Hermit)
on Apr 04, 2006 at 12:07 UTC ( #541133=note: print w/replies, xml ) Need Help??

in reply to my $x or my ($x)

AFAIK the list context just allows you to declare a whole bunch of vars in one go - e.g.

my $foo; my $bar;


my ($foo, $bar);

are the same.

Tom Melly,

Replies are listed 'Best First'.
Re^2: my $x or my ($x)
by jhourcle (Prior) on Apr 04, 2006 at 12:15 UTC

    Although it's convenient that you can declare a whole lot of variables in one line, the big advantage is that you can handle assignments at the same time:

    my ($foo, $bar) = @array;

    The only warning I can give is that using an undef in the list will throw warnings (errors?) in older versions of perl:

    my (undef, $foo, undef, $bar) = @array;

    You can assign values from a list of scalars, as well, but I think it's less legible than assigning one at a time:

     my ($foo, $bar, $baz) = (27, 'blah', $x);

      One common mistake that is made when assigning to a collection of scalars is this:

      my ($one, $two, $three, $four) = 0;

      People tend to think that all four variables are initialized, but really only $one is set to zero, the rest are still undef. You'd have to explicitly set each variable to achieve that result:

      my $one = my $two = my $three = my $four = 0;

      ... or alternatively:

      my ($one, $two, $three, $four) = (0,0,0,0);

      But as was stated elsewhere, this lacks readability for large collections of scalars. In general, I tend to declare my variables and either initialize them to zero or leave them undefined, then assigning their values in later statements. I hate getting warnings about variables being undef when evaluating in a conditional statement.

      No good deed goes unpunished. -- (attributed to) Oscar Wilde
        Just a quick addition to ptum's. I would write:
        my ($one, $two, $three, $four) = (0,0,0,0);
        my ($one, $two, $three, $four) = (0) x 4;
      The only warning I can give is that using an undef in the list will throw warnings (errors?) in older versions of perl:
      my (undef, $foo, undef, $bar) = @array;

      Never fear. Just move your my.
      (undef, my $foo, undef, my $bar) = @array;

      This is particularly more useful in cases where you already have a declared variable.
      my $foo = "ab"; (my $avar, $foo) = ($foo =~ /(.)(.)/);

      my @a=qw(random brilliant braindead); print $a[rand(@a)];

        I prefer slicing. Instead of:

        my (undef, $foo, undef, $bar) = @array;


        (undef, my $foo, undef, my $bar) = @array;


        my ($foo, $bar) = @array[1,3];


        my ($size, $mtime) = (stat $filename)[7,9];

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://541133]
[johngg]: Yes, must be nearly 30 years ago.
[hippo]: Loudest gig I can recall was Wolfsbane at the Astoria in the early 90s. Head rang for about 3 days afterwards. They were top, though.
[johngg]: My ears are still ringing!
Discipulus was outside The Clash concert in 1985..
[marto]: Wolfsbane , now I'm having flashbacks
[choroba]: Isn't Using PerlPod Creatively rather a meditation?
[choroba]: I don't see a question

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2017-03-24 12:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (301 votes). Check out past polls.