Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > printing code references

Reply
Thread Tools

printing code references

 
 
Stuart Kendrick
Guest
Posts: n/a
 
      08-26-2004
how do i print the name of the subroutine to which a code ref points?

#!/opt/vdops/bin/perl
use strict;
use warnings;

my $ref;
$ref = \&foo;
print "&$ref\n";

sub foo {
# Do nothing
}

guru% ./test
&CODE(0x815bed0)
guru%


I would like to see "&foo" instead of "&CODE(0x815bed0)".

--sk

stuart kendrick
fhcrc
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-26-2004
Stuart Kendrick <> wrote in comp.lang.perl.misc:
> how do i print the name of the subroutine to which a code ref points?
>
> #!/opt/vdops/bin/perl
> use strict;
> use warnings;
>
> my $ref;
> $ref = \&foo;
> print "&$ref\n";
>
> sub foo {
> # Do nothing
> }
>
> guru% ./test
> &CODE(0x815bed0)
> guru%
>
>
> I would like to see "&foo" instead of "&CODE(0x815bed0)".


You can't. A sub can be anonymous, it can also be reachable through
more than one name. In the first case, there simply is no name, in
the second, which one should it print?

When a sub is called, the name though which it has been called is
available through the caller() function, but that's doesn't help
with a static coderef. Short of a comprehensive search through
all packages, there is no way to determine if a coderef has names.
(It can be done, and since it can be done, there must be a module...)

Anno
 
Reply With Quote
 
 
 
 
M.J.T. Guy
Guest
Posts: n/a
 
      09-08-2004
Anno Siegel <> wrote:
>Stuart Kendrick <> wrote in comp.lang.perl.misc:
>>
>> I would like to see "&foo" instead of "&CODE(0x815bed0)".

>
>You can't. A sub can be anonymous, it can also be reachable through
>more than one name. In the first case, there simply is no name, in
>the second, which one should it print?


You can, actually - the debugger manages it. It just makes up a name
for anon subs:

DB<1> sub a { print "Hello" }

DB<2> $a = \&a

DB<3> x $a
0 CODE(0x31f17
-> &main::a in (eval 6)[/home/mjtg/perl-5.8.1-RC4/lib/perl5db.pl:618]:2-2
DB<4> $a = sub { print "Goodbye" }

DB<5> x $a
0 CODE(0x336c30)
-> &main::__ANON__[(eval 10)[/home/mjtg/perl-5.8.1-RC4/lib/perl5db.pl:618]:2] in (eval 10)[/home/mjtg/perl-5.8.1-RC4/lib/perl5db.pl:618]:2-2
DB<6>

To see how the trick is performed, rummage in the source of the debugger.


Mike Guy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 2 11-27-2010 12:12 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57