It seems to me that no one has quite described the behavior of split in scalar context accurately yet.

First, it should be noted that there is no such thing as array context in Perl. The only contexts are scalar, list, and void (which is a special kind of scalar context). Scalar has several subcontexts, including string, numeric, and boolean. (It's generally accepted that wantarray was misnamed, and should have been wantlist instead.)

split is almost always used in a list context, where it returns a list of substrings split out from the target string. However, when split is used in a scalar context, as in   $count = split ' ', $string split instead returns the size of the list of substrings. At the same time, the list of substrings is placed in the array @_. In other words, the behavior is effectively   @_ = split ' ', $string; $count = @_;

This implicit assignment to @_ by split in scalar context is deprecated, which is why you receive a warning. You can avoid the warning by using a temporary array.   $count = @tmp = split ' ', $string

However, you don't want the count of elements; you want a reference to an array, which you must ask for explicitly.   $ref = [ split ' ', $string ] The anonymous array constructor puts split in list context (remember, there is no such thing as array context) and returns a reference to an array of substrings.


In reply to Re: list vs array context: odd error message... by chipmunk
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.