|laziness, impatience, and hubris|
Re: Newbie: query about array assignmentby sundialsvc4 (Monsignor)
|on Sep 13, 2012 at 15:19 UTC||Need Help??|
A really great way to find these things out is with a “Perl one-liner,” such as the following: (shown with its output)
... versus ...
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...)
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:
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:
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: