Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Newbie: query about array assignment

by sundialsvc4 (Abbot)
on Sep 13, 2012 at 15:19 UTC ( #993509=note: print w/replies, xml ) Need Help??

in reply to Newbie: query about array assignment

A really great way to find these things out is with a “Perl one-liner,” such as the following:   (shown with its output)

perl -e 'my @stuff="This","That","Other"; use Data::Dumper; print Dumper(\@stuff);' $VAR1 = [ 'This' ];

... versus ...

perl -e 'my @stuff=("This","That","Other"); use Data::Dumper; print Dumper(\@stuff);' $VAR1 = [ 'This', 'That', 'Other' ];

Okay then, what is the difference here?   To find that out, let’s go to perldoc perlop, then search for Comma Operator, where we read in part:   (emphasis mine...)

Comma Operator:

Binary "," is the comma operator.   In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value.   This is just like C’s comma operator.

In list context, it’s just the list argument separator, and inserts both its arguments into the list.  These arguments are also evaluated from left to right.

In the Perl language, “context” is everything.   And so are the constructs use strict; use warnings; which will warn you about a lot of things.   For example, if we add this to it, look what we get now:

perl -e 'use strict; use warnings; my @stuff="This","That","Other"; use Data::Dumper; print Dumper(\@stuff);' Useless use of a constant in void context at -e line 1. Useless use of a constant in void context at -e line 1. $VAR1 = [ 'This' ];

Whoa!   Perl is now warning us about something, but only because we asked it to.   It is now explaining why the result might not have been what you expected from a reading of the perldoc paragraph cited above.   Also note (not shown) that it makes no quibble at all in the second case when the parentheses are inserted.

The lesson here is that you must use strict; use warnings;, all the time.   Perl is designed to “DWIM = Do What I Mean” with a minimum of fuss, but sometimes it does not properly know “what you mean.”

Now, there is one very small but very important thing that I wish to point out to you:   the back-slash that is used in print Dumper(\@stuff);.   Because I know that @stuff is an array, I do not want Perl to “flatten” that array into multiple parameters; I want to pass one reference to the whole array, so that I properly see it as what it is.   Consider what happens if I (errnoneously) omit it:

perl -e 'my @stuff=("This","That","Other"); use Data::Dumper; print Dumper(@stuff);' $VAR1 = 'This'; $VAR2 = 'That'; $VAR3 = 'Other';

Here, Perl “flattened” the parameter-list to the Dumper call, so that it was equivalent to Dumper('This', 'That', 'Other') and so this is how Dumper responded.   But it no longer shows the structure of the array-variable, which is why it’s erroneous.

Last but not least, if you omit the use Data::Dumper; call, or if you do not have that package installed on your system, you’ll get this:

Undefined subroutine &main::Dumper called at -e line 1.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993509]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2016-10-26 11:16 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (340 votes). Check out past polls.