in reply to What CODE typeglob slot is my anonymous sub in?

You want Devel::Peek:
#!/usr/bin/perl use Devel::Peek; sub foo { @_ }; $sub = \&foo; print Devel::Peek::CvGV($sub); __END__ *main::foo

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: What CODE typeglob slot is my anonymous sub in?
by Ovid (Cardinal) on Apr 24, 2007 at 10:23 UTC

    Nice idea, but that doesn't work. The following minimal test case prints '*main::__ANON__'.

    #!/usr/bin/perl use strict; use warnings; use Devel::Peek; my @types = qw( text ); foreach my $type (@types) { my $function = uc $type; my $type_sub; $type_sub = sub { warn Devel::Peek::CvGV($type_sub); }; no strict 'refs'; *$function = $type_sub; } *TINYTEXT = \&TEXT; TINYTEXT();

    Did I misunderstand?

    Cheers,
    Ovid

    New address of my CGI Course.

      The other way round... you *first* have to allocate the typeglob, and *then* get a reference from it to pass into the closure:
      #!/usr/bin/perl use strict; use warnings; use Devel::Peek; my @types = qw( text ); foreach my $type (@types) { my $function = uc $type; my $type_sub; no strict 'refs'; $type_sub = \&$function; *$function = sub { warn Devel::Peek::CvGV($type_sub); }; } *TINYTEXT = \&TEXT; TINYTEXT(); __END__ *main::TEXT at ovid.pl line 18.

      Is that what you want?

      If you assign an anonymous sub to a scalar and stuff that into the code slot, it remains __ANON__.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        Nope. When I call TEXT(), I'd like 'text' reported and when I call 'TINYTEXT()', I'd like 'tinytext' reported. However, I do like what you did. Gives me ideas for this.

        The reason I don't think my problem is really solvable is because I can assign an anonymous sub to thirteen different 'real' subs and I can't tell from the anonymous sub which is getting invoked at that time.

        Cheers,
        Ovid

        New address of my CGI Course.