Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: undef vs empty string '' from split

by Joost (Canon)
on Jun 21, 2007 at 01:39 UTC ( #622446=note: print w/ replies, xml ) Need Help??


in reply to Re^2: undef vs empty string '' from split
in thread undef vs empty string '' from split

Hmm.. You mean like this:

#!perl -w use strict; $_ = 'event_handler='; my ($var1, $var2) = split( /=/); if( !defined($var2) ) { print "undefined\n"; } else { print "'$var1' '$var2'\n"; }
output:
'event_handler' ''
that does seem to be inconsistent with the docs. And what's even more confusing is that:

#!perl -w use strict; $_ = 'event_handler='; my @arr = split(/=/); my ($var1, $var2) = @arr; if( !defined($var2) ) { print "undefined\n"; } else { print "'$var1' '$var2'\n"; }
output:
undefined
Confirms the docs.

updated: added output for perl 5.8.8 / linux


Comment on Re^3: undef vs empty string '' from split
Select or Download Code
Re^4: undef vs empty string '' from split
by Sidhekin (Priest) on Jun 21, 2007 at 05:32 UTC

    split says:

    When assigning to a list, if LIMIT is omitted, or zero, Perl supplies a LIMIT one larger than the number of variables in the list,

    So while @arr = split gets a LIMIT of 0 (and stripping of trailing null fields), ($x, $y) = split gets a LIMIT of 3 (and no such stripping).

    Another special case — potentially (as you, um, realized) surprising — but at least documented.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://622446]
help
Chatterbox?
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: (13)
As of 2015-07-06 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls