The DEBUG constant?

K

kj

In perl documentation, I often run into references to a "DEBUG
constant", or some such. E.g., in the documentation for Carp::Assert
one sees code like this:

assert(EXPR) if DEBUG;

Is there in fact a special DEBUG constant in Perl, or does code
like the example above simply imply that elsewhere in the current
package one has a line like

use constant DEBUG => 0;

?

The above definition of DEBUG through the constant pragma is
straighforward enough but, as far as I can tell, it has the major
disadvantage of requiring the editing of the code to toggle its
value (as opposed to being easily settable from the command-line,
or perhaps from a config file), since all too often programmers
like me will forget to re-edit the code to ensure that DEBUG is
false in production code. Is this assessment correct?

Thanks!

kj
 
P

Paul Lalli

kj said:
In perl documentation, I often run into references to a "DEBUG
constant", or some such. E.g., in the documentation for Carp::Assert
one sees code like this:

assert(EXPR) if DEBUG;

Is there in fact a special DEBUG constant in Perl, or does code
like the example above simply imply that elsewhere in the current
package one has a line like

use constant DEBUG => 0;

?

That is my take on it, yes.
The above definition of DEBUG through the constant pragma is
straighforward enough but, as far as I can tell, it has the major
disadvantage of requiring the editing of the code to toggle its
value (as opposed to being easily settable from the command-line,
or perhaps from a config file), since all too often programmers
like me will forget to re-edit the code to ensure that DEBUG is
false in production code. Is this assessment correct?

Yes.

I tend to use Getopt::Long when I want to enable or disable debug
statements....

use Getopt::Long;
GetOptions('debug' => \my $DEBUG);

assert($foo eq $bar) if $DEBUG;

__END__

And then on the command line...

../assertions.pl --debug
or even just
../assertions.pl -d

Paul Lalli
 
A

anno4000

Paul Lalli said:
That is my take on it, yes.


Yes.

To the OP: You can add unconditional debugging output to remind yourself
to switch off debugging, for instance (untested)

END { warn "Debugging active" if DEBUG }

There may be a form of assertion that can be used instead of warn().

Perl will go out of its way to execute an END block, so you'll
always see the final warning while debugging is on. An uncaught
exception is the only way to bypass END. There is little danger
of accidentally releasing software in that state.
I tend to use Getopt::Long when I want to enable or disable debug
statements....

use Getopt::Long;
GetOptions('debug' => \my $DEBUG);

assert($foo eq $bar) if $DEBUG;

__END__

And then on the command line...

./assertions.pl --debug
or even just
./assertions.pl -d

One point about the the approach using constants is that a statement
qualified by "if DEBUG" will not even be compiled when debugging
is off. Thus code with "DEBUG => 0" will be the same as if no
assertions were present at all.

Anno
 
K

kj

In said:
To the OP: You can add unconditional debugging output to remind yourself
to switch off debugging, for instance (untested)
END { warn "Debugging active" if DEBUG }
There may be a form of assertion that can be used instead of warn().
Perl will go out of its way to execute an END block, so you'll
always see the final warning while debugging is on. An uncaught
exception is the only way to bypass END. There is little danger
of accidentally releasing software in that state.
One point about the the approach using constants is that a statement
qualified by "if DEBUG" will not even be compiled when debugging
is off. Thus code with "DEBUG => 0" will be the same as if no
assertions were present at all.

This is a generic enough functionality that I wish Perl had a
builtin mechanism for it, e.g. a standard global variable (requiring
no package qualification) that the compiler would understand as an
explicit cue from the programmer to optimize code away. This is
one area in which the conflict between "strict" and unqualified
names becomes annoying...

Maybe one can use the code

{ package DE; use constant BUG => 1 }

assert( tongue_in_cheek() ) if DE::BUG;

....

kj
 
B

Brian Greenfield

I always wondered:

Can you, for instance, write

assert( condition );

in the code, but let have perl ignore the statement, or let have perl
_not_ evaluate the arguments, unless a $debug (non-constant) is true?

not quite. Consider x.pl:

#!/usr/bin/perl

use constant DEBUG => 0;

assert( 1 == 0 );

sub assert { die "bad thing happened" if DEBUG && ! $_[0] }

and then deparse it:

~/scripts$ perl -MO=Deparse x.pl # letter O, not digit
use constant ('DEBUG', 0);
assert(!1);
sub assert {
0;
}

you can see the sub call is still compiled, but the contents of the
sub aren't
With a preprocessor one could write, e.g.:

#define assert(a) if(debug) _assert(a)

Is something like this possible in plain perl, too?

No, but I wish it was!
 
D

Dr.Ruud

ska schreef:
Can you, for instance, write
assert( condition );
in the code, but let have perl ignore the statement, or let have perl
_not_ evaluate the arguments, unless a $debug (non-constant) is true?

You can "use Sub::Assert", but the statement is not fullly ignored when
assertions are turned of.

See also "use constant", after which you could write "assert ... if
DEBUG" to let the assert-line get optimized away.

There is also Carp::Assert. And Perl 5.9.

http://search.cpan.org/search?m=module&q=assert&n=50

`perldoc constant`
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top