in reply to undef vs empty string '' from split

You're specifying a limit of 2. You can make 2 fields out of the string "bla=" splitting on = - the last field is the empty string following the = sign.

If you hadn't specified the limit, you would have got what you expected:

Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.)

...

If LIMIT is unspecified or zero, trailing null fields are stripped (which potential users of "pop" would do well to remember).

(from split, emphasis mine). Note that using a limit of 1 does not split the string (which was news to me):

#!/usr/bin/perl -w use strict; sub pr { print "'$_[0]' (no limit) => [".join(",",map { defined $_ ? "'$_'" + : 'undef' } split /=/,$_[0])."]\n" ; print "'$_[0]' (limit 1) => [".join(",",map { defined $_ ? "'$_'" +: 'undef' } split /=/,$_[0],1)."]\n" ; print "'$_[0]' (limit 2) => [".join(",",map { defined $_ ? "'$_'" +: 'undef' } split /=/,$_[0],2)."]\n" ; } pr(''); pr('a'); pr('='); pr('a='); pr('a=b');
update: output:
'' (no limit) => [] '' (limit 1) => [] '' (limit 2) => [] 'a' (no limit) => ['a'] 'a' (limit 1) => ['a'] 'a' (limit 2) => ['a'] '=' (no limit) => [] '=' (limit 1) => ['='] '=' (limit 2) => ['',''] 'a=' (no limit) => ['a'] 'a=' (limit 1) => ['a='] 'a=' (limit 2) => ['a',''] 'a=b' (no limit) => ['a','b'] 'a=b' (limit 1) => ['a=b'] 'a=b' (limit 2) => ['a','b']
updated again fixed code & output.

Replies are listed 'Best First'.
Re^2: undef vs empty string '' from split
by chrism01 (Friar) on Jun 21, 2007 at 01:18 UTC
    I haven't tried your more exotic version, but if you remove the limit cnt from my simple code, you still get the empty string...
    Actually, that's the first thing I tried after it didn't 'work', given that my data only has var=val (if there is a val to get)
      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

        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!