|
|
| The stupid question is the question not asked | |
| PerlMonks |
Re^4: Generate the perl sequence 1, 11, 111, ....by blazar (Canon) |
| on Oct 11, 2008 at 10:10 UTC ( #716580=note: print w/ replies, xml ) | Need Help?? |
|
On the other hand, it looks to me that map is somewhat more magical than the examples in perlsub; if you callit returns an array of coderefs (references to subroutines) which is not at all what is needed here. I personally believe that indeed map and its sibling grep are more magical: precisely because as I wrote in my reply to the OP they accept an expression to be evaluated for each of the other parameters. But I don't understand what you mean with "which is not at all what is needed here" because your example does exaclty what I mean: I do expect it to return a list (not an array!) of coderefs. Only, it probably does not do what you expect in that those coderefs are... all the same coderef:
This is because $_ is a package variable and those anonymous subs are not closures: when you will use one of them, its $_ will be that in scope at the moment, not the one passed to map() when it was created. To obtain that you have to close over a lexical:
Alternatively, and this is the interesting point to be noted here, Perl 5.10 and highter support lexical $_:
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||