9

Have a simple script p.pl:

use strict;
use warnings;
our $x;
BEGIN {
    $x = 42;
}
print "$x\n";

When I run it as:

perl -d:Trace p.pl

prints:

>> p.pl:3: our $x;
>> p.pl:7: print "$x\n";
42

how to get printed the BEGIN block statements too, e.g. the $x = 42;?

Because my intention isn't clear, adding the clarification:

Looking for ANY way to print statements when the perl script runs (like Devel::Trace it does) but including the statements in the BEGIN block.

2
  • 2
    @toolic why wouldn't it? It certainly prints an END block. But probebly it's not completely loaded at the time the BEGIN is executed.
    – simbabque
    Jul 1, 2015 at 13:26
  • 1
    Yes. So, could be more precise. How get printed the BEGIN block statements (like with the -d:Trace) but in ANY other way? (e.g. by using the DB:: or such?)
    – kobame
    Jul 1, 2015 at 13:27

2 Answers 2

8

It's very possible. Set $DB::single in an early BEGIN block.

use strict;
use warnings;
our $x;
BEGIN { $DB::single = 1 }

BEGIN {
    $x = 42;
}
print "$x\n";

$DB::single is a debugger variable used to determine whether the DB::DB function will be invoked at each line. In compilation phase it is usually false but you can set it in compilation phase in a BEGIN block.

This trick is also helpful to set a breakpoint inside a BEGIN block when you want to debug compile-time code in the standard debugger.

2
  • Thank you. One question. Where is this documented?
    – kobame
    Jul 1, 2015 at 19:11
  • Funny how I read past this when researching my answer. Didn't occur to me to try it though. :)
    – simbabque
    Jul 2, 2015 at 6:54
4

Disclaimer: This is just an attempt to explain the behaviour.


Devel::Trace hooks up to the Perl debugging API through the DB model. That is just code. It installs a sub DB::DB.

The big question is, when is that executed. According to perlmod, there are five block types that are executed at specific points during execution. One of them is BEGIN, which is the first.

Consider this program.

use strict;
use warnings;
our ($x, $y);

BEGIN { $x = '42' }
UNITCHECK { 'unitcheck' }
CHECK { 'check' }
INIT { 'init' }
END { 'end' }

print "$x\n";

This will output the following:

>> trace.pl:8: INIT { 'init' }
>> trace.pl:3: our ($x, $y);
>> trace.pl:11: print "$x\n";
42
>> trace.pl:9: END { 'end' }

So Devel::Trace sees the INIT block and the END block. But why the INIT block?

Above mentioned perlmod says:

INIT blocks are run just before the Perl runtime begins execution, in "first in, first out" (FIFO) order.

Apparently at that phase, the DB::DB has already been installed. I could not find any documentation that says when a sub definition is run exactly. However, it seems it's after BEGIN and before INIT. Hence, it does not see whatever goes on in the BEGIN.

Adding a BEGIN { $Devel::Trace::TRACE = 1 } to the beginning of the file also does not help.

I rummaged around in documentation for perldebug and the likes, but could not find an explanation of this behaviour. My guess is that the debugger interface doesn't know about BEGIN at all. They are executed very early after all (consider e.g. perl -c -E 'BEGIN{ say "foo" } say "bar"' will print foo.)

3
  • For what it's worth anyway.
    – simbabque
    Jul 1, 2015 at 13:54
  • 2
    +1, thank you for the explanation. This is first time when i saw an answer like: "it is impossible". ;) Usually, for the "impossible" questions got answers, like: it *is possible* but only by brutal hacking ... :) :D
    – kobame
    Jul 1, 2015 at 14:03
  • 3
    I didn't say it's impossible. I just don't know how. :-P
    – simbabque
    Jul 1, 2015 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.