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

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

Thread Previous
Dave Mitchell
January 6, 2016 16:30
Re: [perl #127083] Caller inside BEGIN block return wrong info
Message ID:
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:

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


    #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]";

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

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

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

    MAIN BEGIN: /tmp/t:16
    MOD BEGIN:  /tmp/
    MOD:        /tmp/
    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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About