ELISHEVA has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: map and return
by ambrus (Abbot) on Sep 03, 2009 at 10:51 UTC | |
|
Re: map and return
by merlyn (Sage) on Sep 03, 2009 at 10:14 UTC | |
by ELISHEVA (Prior) on Sep 03, 2009 at 10:40 UTC | |
by Anonymous Monk on Sep 03, 2009 at 11:22 UTC | |
by ELISHEVA (Prior) on Sep 03, 2009 at 12:08 UTC | |
by Anonymous Monk on Sep 03, 2009 at 12:27 UTC | |
| |
|
Re: map and return
by ikegami (Patriarch) on Sep 03, 2009 at 13:58 UTC | |
by ELISHEVA (Prior) on Sep 03, 2009 at 14:50 UTC | |
by ikegami (Patriarch) on Sep 03, 2009 at 17:09 UTC | |
|
Re: map and return
by LanX (Saint) on Sep 03, 2009 at 16:12 UTC |