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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.