in reply to Surprise: scalar(($x, $y) = split)

Update: Everything below is wrong, as ambrus mentions in the reply. (Note to self: Self, just because you get the correct result doesn't mean that you got the right answer!)

ambrus:

You forgot some parenthesis:

(2==(my($x,$y) = split " ")) or die "wrong number of fieldsd";

Example:

$ cat foo.pl use strict; use warnings; my $x; my $y; while (<DATA>) { chomp; (2==0+(($x, $y) = split /:/,$_)) or print "wrong number of fields\ +n"; print "<$_>, <$x>, <$y>\n"; } __DATA__ FOO:BAR:BAZ:PLOVE FOO:BAR:BAZ FOO:BAR BAZ $ perl foo.pl wrong number of fields <FOO:BAR:BAZ:PLOVE>, <FOO>, <BAR> wrong number of fields <FOO:BAR:BAZ>, <FOO>, <BAR> <FOO:BAR>, <FOO>, <BAR> wrong number of fields Use of uninitialized value $y in concatenation (.) or string at foo.pl + line 7, <DATA> line 4. <BAZ>, <BAZ>, <>

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: Surprise: scalar(($x, $y) = split)
by ambrus (Abbot) on Oct 28, 2011 at 14:05 UTC

      ambrus:

      Ha, ha--color me surprised as well!

      Corrected example:

      $ cat foo.pl use strict; use warnings; my $x; my $y; while (<DATA>) { chomp; 2==(($x, $y) = split " ") or print "wrong number of fields\n"; print "<$_>, <$x>, <$y>\n"; } __DATA__ FOO BAR BAZ PLOVE FOO BAR BAZ FOO BAR BAZ $ perl foo.pl wrong number of fields <FOO BAR BAZ PLOVE>, <FOO>, <BAR> wrong number of fields <FOO BAR BAZ>, <FOO>, <BAR> <FOO BAR>, <FOO>, <BAR> wrong number of fields Use of uninitialized value $y in concatenation (.) or string at foo.pl + line 7, <DATA> line 4. <BAZ>, <BAZ>, <>

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.