The first thing one must realize is that
BEGIN is a special
subroutine.
sub BEGIN { ... }. But for certain subroutines (
BEGIN END CHECK INIT DESTROY, did I miss any?) no
sub keyword is needed. Now it's clear that one can't write
DEBUG && sub BEGIN { ... } and expect it to be sane. But yes, it does indeed compile. This is when you come to hate indirect object syntax even more.
The indirect syntax allows a block as it's first argument (so you can resolve the object, look at the documentation for
print.) This means you can write
method BLOCK and optionally have some arguments. Now, in
DEBUG && ... the dots are expected to be an expression, not a sub declaration. So perl does what you asked it to: it parses an expression.
BEGIN { ... } is an expression. It's indirect object syntax, and
BEGIN is the method.
do { ... } -> BEGIN
Tricky eh?
Now when we've got that straight we need to do what we want in another way. As already said you should use
require and
import instead of
use. And since a
BEGIN is a subroutine we can
return from it.
use constant DEBUG => 1;
BEGIN {
return unless DEBUG;
require CGI::Carp;
import CGI::Carp qw/ fatalsToBrowser /;
}
Cheers,
-Anomo