Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: how to get alias name of anonymous sub?

by ruoso (Curate)
on Nov 29, 2010 at 10:24 UTC ( #874220=note: print w/ replies, xml ) Need Help??


in reply to how to get alias name of anonymous sub?

As noone seem to have pointed already, there's one important module on that subject which is Sub::Name. It should allow you to:

use Sub::Name foreach (keys %news_sites) { *{$_} = subname "generator_for_$_" => sub {...} }

But note that you seem to be missing one more important concept here, which is the concept of closures

foreach my $key (keys %news_sites) { *{$key} = sub { ... # as the $key variable was declared in the outer scope of the s +ubroutine, # if the code reference survives longer than this scope it will + retain its # value warn $key; # this will warn the value used when defining this s +ub. } }
daniel


Comment on Re: how to get alias name of anonymous sub?
Select or Download Code
Re^2: how to get alias name of anonymous sub?
by gian (Novice) on Nov 29, 2010 at 16:49 UTC
    I really missed the closures.Thanks for your remind.I heard the concept of closures before,but I could't find the usage of it in perl , so i did't understand it very much . I think this a good example for me.Thanks ruoso.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://874220]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (19)
As of 2015-07-02 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls