in reply to Re^2: coderefs and (&) prototypes
in thread coderefs and (&) prototypes

Thanks very much for the clarification. I keep forgetting that the sub passed to map operates on $_ rather than receiving its argument in @_, so I'm always writing something like map &f, qw/1 2 3/ and wondering why I don't get (f(1), f(2), f(3)).

For your second example, though, when I tested it, the code

print(( map &{ get_cb() }, qw/a b c/), "\n");
printed ABC, just as mymap did. Am I still missing something?

Replies are listed 'Best First'.
Re^4: coderefs and (&) prototypes
by ikegami (Patriarch) on Jul 28, 2009 at 18:28 UTC
    sub mymap (&@) { my ( $code, @list ) = @_; map &$code, @list } my @cbs; sub reset_cbs { @cbs = ( sub { uc }, sub { lc } ); } sub get_cb { push @cbs, shift(@cbs); return $cbs[0]; } reset_cbs(); print(( mymap \&{ get_cb() }, qw( a b c d ) ), "|"); print(( mymap \&{ get_cb() }, qw( a b c d ) ), "\n"); reset_cbs(); print(( map &{ get_cb() }, qw( a b c d ) ), "|"); print(( map &{ get_cb() }, qw( a b c d ) ), "\n");
    abcd|ABCD aBcD|aBcD