Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) 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)

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.


In reply to Re: Newbie: query about array assignment by sundialsvc4
in thread Newbie: query about array assignment by darren_uk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (3)
    As of 2014-07-13 00:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (242 votes), past polls