in reply to Re: syntax of map operator
in thread syntax of map operator
I since discovered that both versions add a scope.Yes, but they aren't the same scopes. The EXPR variant doesn't create a lexical scope; the BLOCK variant does:
I'm not quite sure what kind of scoping effects happen.$ perl -Mstrict -wcE 'my @x = map my $x = $_, 2; say $x' -e syntax OK $ perl -Mstrict -wcE 'my @x = map {my $x = $_} 2; say $x' Global symbol "$x" requires explicit package name at -e line 1. -e had compilation errors.
So, if we declare $x in the first argument of map, $x exists afterwards, but doesn't have a defined value. If declared before the map, it keeps the last assigned value.$ perl -Mstrict -wE 'my @x = map my $x = $_, 2; say $x' Use of uninitialized value $x in say at -e line 1. $ perl -Mstrict -wE 'my $x; my @x = map $x = $_, 2; say $x' 2
I guess that's more an artifact of the implementation than that it was designed to work this way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: syntax of map operator
by ikegami (Patriarch) on Jan 24, 2010 at 19:01 UTC | |
by JavaFan (Canon) on Jan 24, 2010 at 22:48 UTC | |
by Anonymous Monk on Jan 25, 2010 at 04:15 UTC |