in reply to How to fool caller() / use NEXT within a dynamic sub

#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; sub printcaller { return ( caller(1) )[3]; } my $package = "testpackage"; { my $func_name = $package . '::function'; my $sub = sub { local *__ANON__ = $func_name; printcaller() }; no strict 'refs'; *{ $func_name } = $sub; } is( testpackage::function(), 'testpackage::function', 'tricked caller( +)!' );

Replies are listed 'Best First'.
Re^2: How to fool caller() / use NEXT within a dynamic sub
by betterworld (Curate) on Jul 26, 2005 at 20:35 UTC
    Thanks, this is really cool! Took me a while to understand it, though.
    I think I should write something like "package Dummy;" before messing around with the *__ANON__ typeglob to make sure not to pollute the namespace.
Re^2: How to fool caller() / use NEXT within a dynamic sub
by xdg (Monsignor) on Jul 26, 2005 at 20:39 UTC

    OK, that's really cool, but Huh? Is there any documentation for that somewhere? (Other than reading the perl source code, I guess.)

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      I haven't seen any documentation outside of reading gv.c. Ovid is the one who gave me the tip. My guess is that someone fooled around with Devel::Peek and found that anonymous globs have a special case to give them stash names.

      Is there any documentation for that somewhere?

      Well, as I indicated in my first posting in this thread, caller() would usually return something::__ANON__. So I guess that chromatic's idea is basically, 'Why not just make __ANON__ an alias to what we need?'.

      I don't know if this is documented; and probably I wouldn't have been able to work this out myself.