printing code references

Discussion in 'Perl Misc' started by Stuart Kendrick, Aug 26, 2004.

  1. 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
    Stuart Kendrick, Aug 26, 2004
    #1
    1. Advertising

  2. Stuart Kendrick

    Anno Siegel Guest

    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
    Anno Siegel, Aug 26, 2004
    #2
    1. Advertising

  3. Stuart Kendrick

    M.J.T. Guy Guest

    Anno Siegel <-berlin.de> 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(0x31f178)
    -> &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
    M.J.T. Guy, Sep 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    408
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    425
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    581
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    688
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    816
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page