Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Wednesday March 26, 2008
09:58 AM

Strange BEGIN behavior?

[ #35982 ]

In Test::Aggregate, if a test program has a plan, I want to try to verify that it ran that number of tests. Because of how BEGIN and END blocks work, I am forced to override Test::Builder's ok() method. I've wound up with something like this:

my $ok;
BEGIN { $ok = \&Test::Builder::ok }
sub Test::Builder::ok {
    my $callpack;
    my $stack_level = 0;
    while ( my ($package, undef, undef, $subroutine) = caller($stack_level) ) {
        last if 'Test::Aggregate' eq $package;
        last if $callpack && $subroutine =~ /::(?:BEGIN|CHECK|INIT|END)\z/;
        $callpack = $package;
        $stack_level++;
    }
    $TESTS_RUN{$callpack} += 1;
    $ok->(@_);
}

See that strange line that checks if I'm in a BEGIN/CHECK/INIT/END block? That's because this prints out that they all run in main:: (I've updated this code to show the issue better).

#!/usr/bin/env perl -l

use strict;
use warnings;

package Bar;
{

    package Foo;
    BEGIN { print 'BEGIN in ' . caller(0) . ' ' . __PACKAGE__ }
    CHECK { print 'CHECK in ' . caller(0) . ' ' . __PACKAGE__ }
    INIT  { print 'INIT in ' . caller(0) . ' ' . __PACKAGE__ }
    END   { print 'END in ' . caller(0) . ' ' . __PACKAGE__ }

    sub frames {
        my $frame = 0;
        while ( my $callpack = caller($frame) ) {
            print "Frame $frame in $callpack";
            $frame++;
        }
    }
}
Foo->frames;
__END__
BEGIN in main Foo
CHECK in main Foo
INIT in main Foo
Frame 0 in Bar
END in main Foo

I've read perlmod and perlsub, but I can't tell if this is documented. I know they're not really subs and that's why caller doesn't report what I expected, but are these blocks always in main::?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.