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

Don't hide the information in the subroutine name. Instead, just use a closure:

foreach( keys %news_sites ) { my $name= $_; *{$_}= sub { get %news_sites{$name}; ... }; }

Update: Or

foreach( keys %news_sites ) { my $site= $new_sites{$_}; *{$_}= sub { get $site; ... }; }

- tye        

Replies are listed 'Best First'.
Re^2: how to get alias name of anonymous sub? (closure)
by gian (Novice) on Nov 29, 2010 at 07:28 UTC
    Thanks tye .But it seem there is some problem in your solution ,the variable will be assigned according the context where the sub be executed.just like this.
    package test; for (A..D) { *{$_}=sub{ print "my name is $_ \n"; }; } package main; test->A(); test->B(); $_="here"; test->C(); test->D(); output: my name is my name is my name is here my name is here

      No, that is a problem with your code. Closures close over lexicals. $_ is not a lexical. You deleted a line that I inserted quite intentionally and thus broke the code.

      - tye