develooper Front page | perl.perl5.porters | Postings from January 2016

Re: [perl #127083] Caller inside BEGIN block return wrong info

Thread Previous
From:
Dave Mitchell
Date:
January 6, 2016 16:30
Subject:
Re: [perl #127083] Caller inside BEGIN block return wrong info
Message ID:
20160106162955.GH4007@iabyn.com
On Wed, Dec 30, 2015 at 04:31:01AM -0800, KES wrote:
> It is not expected that the information about last frames is changed.
> I think that the call to 'caller' from Mod1 should look like:

Here's a slightly simplified example that only requires a single module:

Mod.pm:

    package Mod;
    BEGIN {
        print "MOD BEGIN:  ", ::top_call(), "\n";
    }
    print "MOD:        ", ::top_call(), "\n";

/tmp/t:

    #return file and line of top-most level call site
    sub top_call {
        my $i = 0;
        my $where;
        while(my @c =  caller($i++)) {
            $where = "$c[1]:$c[2]";
        }
        $where;
    }

    BEGIN {
        print "MAIN BEGIN: ", ::top_call(), "\n";
        require Mod;
    }

    sub f { print "f():        ", ::top_call(), "\n"; }
    f();


This code prints the outermost call-site at various places. It outputs:

    MAIN BEGIN: /tmp/t:16
    MOD BEGIN:  /tmp/Mod.pm:4
    MOD:        /tmp/Mod.pm:0
    f():        /tmp/t:19

I'd expect the middle two to be 

    MOD BEGIN:  /tmp/t:16
    MOD:        /tmp/t:16

instead, since they're both called from the BEGIN in /tmp/t.

Still present in blead.


-- 
O Unicef Clearasil!
Gibberish and Drivel!
    -- "Bored of the Rings"

Thread Previous


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