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.
In reply to Re^2: syntax of map operator
by JavaFan
in thread syntax of map operator
by sman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |