To expand a bit on swiftone's point, the construct you have in the first section asks for and receives a side-effect of the split. split always returns a list (a list of one element is still a list). However, your first construct uses split in scalar context. (I presume internally, split doesn't use any sort of wantarray checking.)

To do what split wants and to do what you want, perl has to assign the list results of the split operation to @_, then evaluate @_ in scalar context, giving you the number of elements. You might as well just use m// with your pattern in scalar context.

Your second one works because you do as swiftone recommends, creating an anonymous array out of the split results. That puts split in list context. Next, you dereference the anonymous array, and evaluate that in scalar context. Same results, except that wrapping the split in the reference/dereference stuff fools perl just enough that it doesn't give you the error message.

As for the error message, it means "Try not to do a potentially expensive operation if you're just going to throw away the results."


In reply to Re: list vs array context: odd error message... by chromatic
in thread list vs array context: odd error message... by jynx

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.