Could you explain where/how do you think split() is inconsistent? . . . Wait, this is where ...
In my original version I was getting a zero length string as the last value as shown in the SSCCE above. But using the more elegant version, it becomes undef and I get the warning shown.
... in which case, sorry to bother you. I see that in perl 5.24.0.
So in your own version, split() gets the number of fields (limit) to generate; in Eily's version, split() behaves as expected in that (just see the B::Deparse output) ...
# Inserted newlines for legibility of one-liner run under Windows.
perl -MO=Deparse -e "
use strict;
use warnings;
my $x = q[a b c d e f ];
my ( $one , undef , undef, undef , undef, undef , $other ) = split q
+[ ] , $x;
my @all = split q[ ] , $x;
"
use warnings;
use strict;
my $x = 'a b c d e f ';
my($one, undef, undef, undef, undef, undef, $other) = split(' ', $x, 8
+);
my @all = split(' ', $x, 0);
-e syntax OK
At least in perl 5.24.0, supplying 0 or -1 as the limit to split() in the OP's version has no effect on the outcome, i.e. $other gets the value of empty string not undef. $other becomes undef only if one sets the limit to 6.
Then force the issue by splitting in list context: my ( $x , ... , $y ) = () = split( ... ).
After all that, I am unable to answer your question: Why?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.