To my mind, with the exception of the suggestion to use unpack and the one to use m///, both which should work, the rest of the above doesn't DWYM. If you want the number to be transferred to the first variable, split won't do that, because it excludes as delimiters whatever matches the regexp. Normally the bracketed part of the regexp would transfer matches to $1, $2 etc. NOT to the list returned by split by the way.

Unfortunately, in the case of running the regexp through split, this will cause match variables to be rendered undefined at the point where split returns.

There might be some nasty trick to force split to abort and leave match variables intact, but I can hardly recommend such an approach.

The OPs implied alternative for regexping without split when coded correctly, would look something like:

$line =~ /^(\d{4})(.*)$/ or die "unexpected content at line $.\n"; my ( $one, $two ) = ( $1, $2 );

-M

Free your mind


In reply to Re: can split() use a regex? by Moron
in thread can split() use a regex? by Anonymous Monk

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.