http://qs321.pair.com?node_id=1230567


in reply to Re^2: Avoiding repeated undefs
in thread [Solved] Avoiding repeated undefs

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?