http://www.perlmonks.org?node_id=11132260


in reply to Attempting to use $_ in "sub" inside "map"

One way, albeit not pretty, is to form a closure | lexical closure (if that's the proper terminology) over the value of the global $_ and use the closed-over value:

Win8 Strawberry 5.8.9.5 (32) Sat 05/08/2021 0:53:55 C:\@Work\Perl\monks >perl use strict; use warnings; my %dispatch = ( normal => sub{"$_[0] Normal dispatch"}, map { my $__ = $_; $_ => sub{"$_[0] Sub returns $__"} } qw| Uno Dos tres| ); print $dispatch{"normal"}->(0),"\n"; # 0 Normal dispatch (as expected +) print $dispatch{"Uno"} ->(1),"\n"; # WANT: "1 Sub returns Uno" print $dispatch{"Dos"} ->(2),"\n"; # WANT: "2 Sub returns Dos" ^Z 0 Normal dispatch 1 Sub returns Uno 2 Sub returns Dos

Update 1: Also see this article on closure.

Update 2: Another way, not involving closures - although closures are not to be scorned!

Win8 Strawberry 5.8.9.5 (32) Mon 05/10/2021 9:33:38 C:\@Work\Perl\monks >perl use strict; use warnings; my %dispatch = ( normal => sub{ "$_[0] Normal dispatch" }, map { $_ => eval qq{ sub{ "$_ returns \$_[0]" } } } qw(Uno Dos tre +s), ); print $dispatch{'normal'}->( 0), "\n"; print $dispatch{'Uno' }->( 1), "\n"; print $dispatch{'Dos' }->( 22), "\n"; print $dispatch{'tres' }->(333), "\n"; ^Z 0 Normal dispatch Uno returns 1 Dos returns 22 tres returns 333


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Attempting to use $_ in "sub" inside "map"
by NetWallah (Canon) on May 08, 2021 at 05:26 UTC
    Thank you (++)! - that works well.

                    "Avoid strange women and temporary variables."

Re^2: Attempting to use $_ in "sub" inside "map"
by LanX (Sage) on May 08, 2021 at 07:55 UTC
    > is to form a closure | lexical closure (if that's the proper terminology)

    I'm pretty sure both are correct. I have trouble to imagine closed over variables which are not lexical. (I usually call those global ;-)

    Personally I'd use "lexical closure" to disambiguate, if there is another interpretation of "closure" in the context.

    The same way we use "file-glob" and "type-glob" to distinguish between glob and the * sigil

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      I have trouble to imagine closed over variables which are not lexical.

      Yeah, I was thinking about it after posting and I couldn't come up with an example of non-lexical closure either. I'll just add a link to the Wikipedia article and let everyone else figure it out. :)


      Give a man a fish:  <%-{-{-{-<

      In languages without lexically scoped variables or something approximating them, there's not really any such thing as a closure. You can still have first-class functions (i.e. coderefs); they just have no need to close over anything.

        > first-class functions (i.e. coderefs)

        Doesn't equal closures. OK PHP is using that terminology, but it's wrong ...

        ... anyway who's using PHP anymore ;-)

        > In languages without lexically scoped variables or something approximating them

        do you have an example for such a language with functions as first class objects?

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery