develooper Front page | perl.perl5.porters | Postings from December 2013

Re: [perl #120696] CORE::caller() function parameter (5th arg) isone level off inside package DB?

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
December 9, 2013 11:42
Subject:
Re: [perl #120696] CORE::caller() function parameter (5th arg) isone level off inside package DB?
Message ID:
20131209114208.GE2490@iabyn.com
On Wed, Dec 04, 2013 at 06:29:21PM -0800, Rocky Bernstein wrote:
> When in package DB and the extended array version of CORE::caller is used,
> the function parameter (the 5th arg) seems to be one function level off
> from the package, file and line values.
> 
> Consider DB module Bug2.pm saved let's say in ./Devel/Bug2.pm
> 
> #!/usr/bin/perl
> 
> package DB;
> sub DB {
>     local *CORE::GLOBAL::caller = sub {
> my $skip = 1;
> while (caller($skip)) {
>     my @c = caller($skip++);
>     print join(', ', @c[0..3]), "\n";
> }
> print "-" x 30, "\n";
>     };
>     eval "caller($skip)";
> };
> 
> package Devel::Bug2;
> 1;
> 
> and program bug1 saved say in ./bug1.pl
> 
> #!/usr/bin/perl
> 
> sub five() {
>     return 5;
> }
> print five(), "\n";
> 
> When I run "perl -I. -d:Bug2 bug1.pl" I get as output:
> 
> perl -d:Bug2 /tmp/bug1.pl
> DB, Devel/Bug2.pm, 13, (eval)
> main, bug1.pl, 6, DB::DB
> ------------------------------
> DB, Devel/Bug2.pm, 13, (eval)
> main, bug1.pl, 4, DB::DB
> main, bug1.pl, 6, main::five
> ------------------------------
> 5
> 
> Note that line bug1.pl line 6 is *not* in function main:five.
> Similarly bug1.pl line 4 is in function main::five no function DB::DB.

This is actually what's supposed to happen. Its a bit confusing,
but the function name returned by caller() is always one-level out:
is shows the function *called* at the caller position, not the function
doing the calling.

For example, this:

    #!/usr/bin/perl
    sub f { 
        my $skip = 0;
        while (caller($skip)) {
            my @c = caller($skip++);
            print join(', ', @c[0..3]), "\n";
        }
    }
    sub g { 
        f(); # line 10
    }
    g(); #line 12

gives this:

    main, /home/davem/tmp/p, 10, main::f
    main, /home/davem/tmp/p, 12, main::g

Line 10 is not in f(), and line 12 is not in g().

-- 
"Emacs isn't a bad OS once you get used to it.
It just lacks a decent editor."

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About