Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^3: Avoiding repeated undefs

by parv (Vicar)
on Feb 26, 2019 at 14:17 UTC ( #1230567=note: print w/replies, xml ) Need Help??

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?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1230567]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-10-22 01:39 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (225 votes). Check out past polls.