Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How/can one to save/restore CORE::caller inside an eval?

by tobyink (Canon)
on Dec 03, 2013 at 22:34 UTC ( #1065502=note: print w/replies, xml ) Need Help??


in reply to How/can one do save/restore CORE::caller inside an eval?

use v5.12; # This is basically a dummy override that must be performed early # on so that Perl knows we'll be overriding CORE::GLOBAL::caller # later... # BEGIN { *CORE::GLOBAL::caller = sub { CORE::caller(@_) } }; sub foo { say scalar caller; } sub bar { local *CORE::GLOBAL::caller = sub { 'xxxx' }; foo(); } foo(); bar();
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^2: How/can one do save/restore CORE::caller inside an eval?
by rockyb (Scribe) on Dec 04, 2013 at 02:47 UTC

    This is fantastic — almost. The first problem I have is that adding the BEGIN code causes Devel::Trepan to run so slowly that I've not had the patience to wait until it is done. I've tried without the BEGIN, and overwriting caller() still works.

    More serious is the fact that my replaced version of caller() needs to call the original caller (after it strips off the call stack entries on top that are inside the debugger). I tried for bar():

        sub bar {
            local *orig_caller = *CORE::GLOBAL::caller;
            local *CORE::GLOBAL::caller = sub { orig_caller($_[0]+1)} # simplified somewhat
        }
    
    
    But I'm getting:
      Deep recursion on anonymous subroutine at .. DB::Eval.pm 
    
    because I guess that glob assignment has the same address.

    Help!

      Would it work to just call CORE::caller() from your override?

        YES! That works for me.

        There is still one wafer-thin problem...

        When I use this in an array context, I am getting the function name (and possibly other further parameters) one level above what the package, file, and line number are. I can code around this, but I also wonder if this is a bug or not. Here is a run using Perl 5.18.1:

        ./bin/trepan.pl example/gcd.pl 3 5
        -- main::(example/gcd.pl:18)
        die sprintf "Need two integer arguments, got %d", scalar(@ARGV) unless
            @ARGV == 2;
        $DB::D[0] = 0
        (trepanpl): @ caller
        $DB::D1 =
        @{[
            [0]  "main",
            1  "example/gcd.pl",
            2  18,
            3  "DB::DB", <---- WRONG!
            4  "",
            5  1,
            6  undef,
            7  undef,
            8  2018,
            9  "UUUUUUUUUUUUUUUU",
            10 undef
        ]}
        (trepanpl): @ caller 1
        $DB::D2 =
        @{<undef>}
        (trepanpl): c gcd
        -> main::(example/gcd.pl:8)
        {
            my ($a, $b) = @_;
        trepanpl: s   # there is a "feature" here in my program where I need to step one more
        x1 main::(example/gcd.pl:9)
            my ($a, $b) = @_;
        (trepanpl): @ caller
        $DB::D2 =
        @{[
            [0]  "main",
            1  "example/gcd.pl",
            2  9,
            3  "DB::DB",  <--- still wrong
            4  "",
            5  1,
            6  undef,
            7  undef,
            8  1762,
            9  "UUUUUUUUUUUUUUUU",
            10 undef
        ]}
        (trepanpl): @ caller 1
        $DB::D3 =
        @{[
            [0]  "main",
            1  "example/gcd.pl",
            2  21,
            3  "main::gcd",
            4  1,
            5  1,
            6  undef,
            7  undef,
            8  2018,
            9  "UUUUUUUUUUUUUUUU",
            10 undef
        ]}
        (trepanpl): @ caller 2
        $DB::D4 =
        @{<undef>}
        (trepanpl): q
        Really quit? (N/y) y
        trepan.pl: That's all, folks...
        

        If you want to try the code yourself, see the github Devel::Trepan repo with a commit after 0babe47e08831a9d5b1099a9971fc2677f604331

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1065502]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2021-01-25 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?