Nothing's changed. The assertion the book is making is that you must ensure that the array is being seen in scalar context, not list context. Think of an array as a container for a list. Inside the container is the list. On the lid is a label that tells how many elements are contained inside. In scalar context, you're looking at the label on the lid. In list context, you're looking at the contents of the container.

To explain the "Do what I mean" behavior you show in your example......
It makes no sense for a list to be operated on by the concatenation operator. The concatenation operator takes a scalar on both sides. Thus, if you were able to call "wantarray" on the concatenation operator you would find that it doesn't "want a list." That fact forces the @foo array to be evaluated in scalar context, and the results of that evaluation to be concatenated with whatever is on the other side of the "." operator. As we all know, the good book says, "There is no such thing as a list in scalar context." When you put an @array somewhere that a scalar is expected, you get the behavior documented for @array in scalar context; you see how many elements it contains.

If, on the other hand, you say, print @foo you will see that print accepts a list (and prints a list). If you could call wantarray (an apparent misnomer with origins in the dark ages when lists were called arrays too) on "print" it would return true. And so, @foo is evaluated in list context when evaluated as an arguement of 'print'.

The most peculiar thing I can think of in all this is the fact that concatenation operator doesn't accept a list while variable interpolation into a string does. I know that "concatenating" lists is what join is for. But to a relative greenie like me, it seems inconsistant. (For example, "This @array interpolates as a list.")

Perhaps our lives are simpler and better because arrays inside of literal strings are seen as arrays in list context, but nevertheless, why that "works" when concatenation doesn't is something that the real experts will have to reveal.


Dave


"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

In reply to Re: Array in scalar context. by davido
in thread Array in scalar context. by the_0ne

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.