It seems really unlikely that I've stumbled onto a Perl bug in something that's been around basically forever. But at the very best this seems to be badly misdocumented. Either that or I'm incapable of reading, which is always possible.

Here's the problem:

split 'x','x'

in list context returns a completely empty list. This happens both in 5.10 and 5.16, and, I suspect, every other version as well.

Seems to me it should be returning ('',''), since 'x' is two empty strings separated by 'x'.

Yes, I'm aware there's all sorts of strange legacy shit in split -- in particular, lots of discretion to throw out empty components when the separator pattern is matching on empty strings, or when it's the special-case " " pattern designed to be backwards compatiable with awk -- but the pattern 'x' doesn't seem to be covered by any of the weird cases and should always be doing positive-width matches.

Nor does changing it to /x/ make any difference.

And perlfunc.pod#split is also really clear that

"An empty leading field is produced when there is a positive-width match at the beginning of EXPR. ...

An empty trailing field, on the other hand, is produced when there is a match at the end of EXPR, regardless of the length of the match (of course, unless a non-zero LIMIT is given explicitly, such fields are removed, as in the last example). ..."

And you'll notice above that I'm not setting any LIMITs.

So what am I missing? Where are my empty leading and trailing fields?

Is there a way to get split to return n components when there are n-1 'x's? (I already know how to do this with regexps, but it looks gross...)


In reply to split tosses away empty components even with positive width separators? by wrog

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.