Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Name of caller if aliased?

by LanX (Saint)
on Feb 25, 2015 at 04:15 UTC ( [id://1117778]=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Precious Monkses!

(caller(0))[3] shows me the name of the currently executed function.

But in case of importing/aliasing it'll be the original one not the alias' name.

Any way to find the name of the alias?

details:

$\="\n"; sub show { print join "\t",(caller(0))[0..3]; }; show(); *func = \&show; func();

prints

/usr/bin/perl -w /tmp/tst.pl main /tmp/tst.pl 6 main::show main /tmp/tst.pl 10 main::show

any straight forward way to get "main::func" in the second case?

(no, parsing the calling line is not an option)

Thankses for your attention! :)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)

PS: Je suis Charlie!

Replies are listed 'Best First'.
Re: Name of caller if aliased?
by Discipulus (Canon) on Feb 25, 2015 at 09:25 UTC
    So you too ended playing with symbol table..:=)

    If i understand, and you know i'm still learning, when caller (and i suspect every other trick) starts, the lookup in the symbol table is done and you cannot access anymore the name: you have the right part of the alias assignation.

    As i understand you want something like (using the web analogy) to $ENV{HOST} or $ENV{REFERRER} to identify the original name or if you had been redirected: I dont think such introspection exists in Perl (i hope i'm wrong).

    I think you can only do the opposite thing: given your show function you can look in the symbol table to collect any enrty that point to the same code.

    L*
    update: for future reader here the ouput of a playground i working on that shows the alias thing:
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      > given your show function you can look in the symbol table to collect any enrty that point to the same code.

      Yes I could parse the stash of the calling package and compare the CODE-slots to identify aliases.

      But apart from the overhead, the results won't be necessarily unique.

      $\="\n"; sub show { print join "\t",(caller(0))[0..3]; }; show(); package Test; *func = \&main::show; *funk = \&main::show; func(); funk();

      /usr/bin/perl -w /tmp/tst.pl main /tmp/tst.pl 6 main::show Test /tmp/tst.pl 13 main::show Test /tmp/tst.pl 14 main::show

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

Re: Name of caller if aliased?
by Mr. Muskrat (Canon) on Feb 25, 2015 at 20:29 UTC

    I don't know how far you want to go to get the names but this seems to work well in my limited testing.

    Foo.pm:
    package Foo; use strict; use warnings; use Sub::Name; # forward declarations so AUTOLOAD can limit what works use subs qw( func funk ); $\ = "\n"; sub show { print join "\t", (caller(0))[0..3]; } sub AUTOLOAD { my ( $name ) = our $AUTOLOAD =~ /::(\w+)$/; return unless __PACKAGE__->can( $name ); # only allow those subs l +isted above my $fullname = __PACKAGE__ . '::' . $name; no strict 'refs'; *{ $fullname } = Sub::Name::subname $fullname => *show; goto &$fullname; } 1;
    foo.pl:
    #!/bin/env perl use strict; use warnings; use Foo(); Foo::show(); Foo::func(); Foo::funk();

    Output:

    main    ./foo.pl        7       Foo::show
    main    ./foo.pl        8       Foo::func
    main    ./foo.pl        9       Foo::funk

      Hi

      Don't wanna install Sub::Name now but I doubt this really works cause you are renaming the original sub.

      Try to call func again and it should show the last name assigned.

      Anyway thanks, I was asking for an easy way, I'll rather stick with generating different closures for each symbol now. :)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

        D'oh. You're absolutely right but I did warn you that I had done minimal testing. :)

Re: Name of caller if aliased?
by Anonymous Monk on Feb 25, 2015 at 08:22 UTC
      > Didn't rockyb ask about this?

      No, he didn't.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1117778]
Approved by BrowserUk
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found