Sartak has asked for the wisdom of the Perl Monks concerning the following question:
Hiya,
perlsub mentions that you can redefine CORE::GLOBAL::builtin and it should redefine that builtin globally (i.e. in all packages).
I'm trying to do this for caller and it isn't working. Here's a test script that demonstrates the problem. Note that glob is successfully redefined.
#!perl use strict; use warnings; use Test::More tests => 2; # hide package Outer (a little stupidly) sub my_caller { my $package = CORE::caller(@_); $package = 'main' if $package eq 'Outer'; return $package; } # redefine both caller (which fails) and glob (which works) *CORE::GLOBAL::caller = \&my_caller; *CORE::GLOBAL::glob = \&my_caller; # actual tests my ($got_caller, $got_glob); Outer::outer(); is($got_caller, 'main', 'redefined caller in all packages'); is($got_glob, 'main', 'redefined glob in all packages'); { package Inner; sub inner { $got_caller = caller(); $got_glob = glob(); } } { package Outer; sub outer { Inner::inner() } } __END__ 1..2 Name "CORE::GLOBAL::caller" used only once: possible typo at test.t li +ne 14. Subroutine CORE::GLOBAL::glob redefined at test.t line 15. not ok 1 - redefined caller in all packages # Failed test 'redefined caller in all packages' # at test.t line 20. # got: 'Outer' # expected: 'main' ok 2 - redefined glob in all packages # Looks like you failed 1 test of 2.
Overriding CORE::GLOBAL::caller seems to work if you do it with dynamic scope (such as in Sub::Uplevel), or it only works within the given package (such as in Hook::LexWrap).
If there's no way to override caller globally, then.. why isn't there? It would come in handy for one of my modules.
Thanks for any advice.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Overriding caller everywhere (BEGIN)
by tye (Sage) on Aug 06, 2007 at 05:02 UTC | |
by xdg (Monsignor) on Aug 06, 2007 at 22:48 UTC | |
by Sartak (Hermit) on Aug 07, 2007 at 00:45 UTC | |
by tye (Sage) on Aug 07, 2007 at 01:42 UTC | |
|
Re: Overriding caller everywhere
by Anonymous Monk on Aug 06, 2007 at 05:33 UTC | |
by chromatic (Archbishop) on Aug 07, 2007 at 00:58 UTC |