Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How to get the name from a reference

by michi (Novice)
on Mar 30, 2009 at 16:49 UTC ( #754182=perlquestion: print w/ replies, xml ) Need Help??
michi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i am looking for a way to get the "name" of a reference. I hope this example explains what i mean:
sub my_sub { print "hello\n"; } my $var = 5; my $subname = some_nice_function(\&my_sub); # should return 'main::my_ +sub' my $varname = some_nice_function(\$var); # should return '$var' or 'va +r'
I looking for a function (or any other way) that returns the string 'main::my_sub' if i pass a reference to my_sub. Is there any way to realize this?

Comment on How to get the name from a reference
Download Code
Re: How to get the name from a reference
by repellent (Priest) on Mar 30, 2009 at 17:12 UTC
    As far as I know, we can only do it for CODE references:
    # obtain CODEref name use B; sub coderef2name { my ($coderef) = @_; return unless UNIVERSAL::isa($coderef, "CODE"); my $obj = B::svref_2object($coderef); return $obj->GV->STASH->NAME . "::" . $obj->GV->NAME; }

    Update: Changed to use GV->STASH->NAME instead.

    Note that:
    > perl -MDevel::Peek -e '$var = 123; Dump \$var; sub my_sub {}; Dump \ +&my_sub' SV = RV(0x8073260) at 0x804cc28 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x8065648 SV = IV(0x8068920) at 0x8065648 REFCNT = 2 FLAGS = (IOK,pIOK) IV = 123 SV = RV(0x8073260) at 0x804cc28 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x8064b34 SV = PVCV(0x8061538) at 0x8064b34 REFCNT = 2 FLAGS = () IV = 0 NV = 0 COMP_STASH = 0x804cb50 "main" START = 0x806d6a8 ===> 1053 ROOT = 0x806fa68 XSUB = 0x0 XSUBANY = 0 GVGV::GV = 0x8065684 "main" :: "my_sub" FILE = "-e" DEPTH = 0 FLAGS = 0x0 OUTSIDE_SEQ = 91 PADLIST = 0x8064b04 PADNAME = 0x804cce8(0x8088758) PAD = 0x8065678(0x805e5b0) OUTSIDE = 0x804cd78 (MAIN)
Re: How to get the name from a reference
by Fletch (Chancellor) on Mar 30, 2009 at 17:17 UTC
Re: How to get the name from a reference
by ikegami (Pope) on Mar 30, 2009 at 17:20 UTC
    What are you trying to accomplish?
Re: How to get the name from a reference
by Marshall (Prior) on Mar 30, 2009 at 20:09 UTC
    I second ikegami's comment: what is the purpose?

    With Perl there are amazing things that you can discover about what is going on in a sub. Just some simple example code is shown below. I am unsure as to what you want.

    #!/usr/bin/perl -w use strict; print "testing main\n"; x(); sub x { print "In sub x\n"; print " I am: ", WHOamI(), "\n"; SubLevel2(); } sub SubLevel2 { print "in sub SubLevel2\n"; print " I am: ", WHOamI(), "\n"; print " I was called by ", WHOwasI(),"\n"; } sub WHOamI { (caller(1))[3] }; #returns name of the #sub that called this sub sub WHOwasI { (caller(2))[3] }; #returns name of the parent of #the sub that called this sub __END__ #prints testing main In sub x I am: main::x in sub SubLevel2 I am: main::SubLevel2 I was called by main::x
Re: How to get the name from a reference
by michi (Novice) on Mar 31, 2009 at 17:28 UTC
    Thanks for your comments. I tried Sub::Info and noticed that this module uses Sub::Identify which does exactly what i need. :)
    The reason why want to do this is that i need to send references to functions to other threads (with queues). Unfortunately its not possible to add strict CODE refs to a queue.
Re: How to get the name from a reference (data dump dumper varname var name)
by Anonymous Monk on Sep 10, 2013 at 09:03 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-12-27 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls