Recently I noticed something odd with map and return. According to perlsub, an initial & in a prototype allows you to define a subroutine that emulates the syntax of Perl built-ins like map and grep. And yet there seem to be subtle differences, like the behavior of return within the block. For instance, in the case of map the block after the subname is scoped as if it were part of the surrounding code rather than the body of an anonymous sub.
The following code, placed inside a subroutine will cause the subroutine to return immediately. If placed at the top level of the script it will cause the Perl compiler to complain "Can't return outside subroutine ..." (Perl version 5.8.8; warnings, strict turned on of course).
my @x = map { return $_; } (1,2,3); print "(@x)\n";
However, when the same block { return $_; } is called via a user defined sub that does essentially the same thing as map there are no compiler complaints nor precipitous returns:
sub foo(&@) { my ($crSub, @aParams) = @_; my @aResult; push(@aResult, $crSub->($_)) foreach @aParams; return @aResult; } #this outputs "(1 2 3)" my @x = foo { return $_; } (1, 2, 3); print "(@x)\n";
perlsub says that the user defined routines parse "almost exactly like". Almost isn't exactly. Why the difference in behavior? And what other subtle differences are there between built-in tokens like map and user defined subroutines with a (&@) prototype? The return behavior of map ate up a good deal of debugging time the other day because I just assumed that the block after map was just like an anonymous subroutine - which it clearly isn't. I'm hoping to save time in the future with a heads up on other differences hidden behind the phrase "almost exactly".
Many thanks in advance, beth
In reply to map and return by ELISHEVA
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |