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.


In reply to Overriding caller everywhere by Sartak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.