Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 chilling in the Monastery: (5)
As of 2014-10-02 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (49 votes), past polls