Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by Joost (Canon)
on Jun 21, 2007 at 01:39 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://622446]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 23:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found