Compiler directives for Perl?

Y

Yash

I have a Perl program that processes millions of records, and hence
has to be efficient. However, during development and testing, we do a
lot of logging to a file for debugging.
The code has a lot of statements such as:
diagnostic("Input line is");
diagnostic($inputLine);

The function is defeined as:
sub diagnostic
{
if ( $DIAGNOSTICS == 1 )
{
printf LOGFILE $_[0];
}
}

When this same program is shipped and executed on the client's system,
even if the $DIAGNOSTICS value is set to 0, the condition checks are
still done, just that no message is logged.
I would ideally like to instruct Perl not to compile any code related
to the diagnostic statements. Is there a way to specify compiler
directives to Perl as is done in C++ compilers?
In c++ I would compile using
cc .... -DDIAG. The code would have
#ifdef DIAG
diagnostic code
#endef

I know Perl is interpreted, but it still does compilation before
running. Is there some way I can pass an argument so that I make it
compile without diagnostic messages?


Thanks
 
G

gnari

Yash said:
I have a Perl program that processes millions of records, and hence
has to be efficient. However, during development and testing, we do a
lot of logging to a file for debugging.
The code has a lot of statements such as:
diagnostic("Input line is");
diagnostic($inputLine);
...

if all your debug are in the above format, then just pass your sources
through a perl -ni.bak -e'print unless /^diagnostic\(/' source.pl >
production.pl

this would be simple to set up in a Makefile
make debug
make production

... Is there a way to specify compiler
directives to Perl as is done in C++ compilers?
In c++ I would compile using
cc .... -DDIAG. The code would have
#ifdef DIAG
diagnostic code
#endef

you might want to look at the Filter modules.
Filter::pp comes to mind.

gnari
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

I have a Perl program that processes millions of records, and hence
has to be efficient. However, during development and testing, we do a
lot of logging to a file for debugging.
The code has a lot of statements such as:
diagnostic("Input line is");
diagnostic($inputLine);
sub diagnostic
{
if ( $DIAGNOSTICS == 1 )
{
printf LOGFILE $_[0];
}
}

In c++ I would compile using
cc .... -DDIAG. The code would have
#ifdef DIAG
diagnostic code
#endef

Well, you could use a constant and then define diagnostic() depending on
the constant.

use constant DIAGNOSTICS => 1;

...

*diagnostic = DIAGNOSTICS ?
sub { print LOGFILE $_[0] } : # why printf()?
sub { };

Or, you could use DIAGNOSTICS on each line that calls diagnostic():

diagnostic($x) if DIAGNOSTICS;

...

sub diagnostic { print LOGFILE $_[0] }

Or, you could go CRAZY and use 'perl -P yourfile.pl', which allows you to
do things like '#ifdef' and '#endif' (it passes your code through the C
preprocessor).

perldoc perlrun
 
T

Tore Aursand

I have a Perl program that processes millions of records, and hence has
to be efficient. However, during development and testing, we do a lot of
logging to a file for debugging. The code has a lot of statements such
as: diagnostic("Input line is");
diagnostic($inputLine);

The function is defeined as:
sub diagnostic
{
if ( $DIAGNOSTICS == 1 )
{
printf LOGFILE $_[0];
}
}

When this same program is shipped and executed on the client's system,
even if the $DIAGNOSTICS value is set to 0, the condition checks are
still done, just that no message is logged. I would ideally like to
instruct Perl not to compile any code related to the diagnostic
statements. Is there a way to specify compiler directives to Perl as is
done in C++ compilers?

By using constants you should probably be able to accomplish this, as
constants used in an expression will be optimized away (if the constant is
false, of course);

use constant DEBUG => 0;

if ( DEBUG ) {
# This will be optimized away
}

Please take a look at 'perldoc constant' for more information.
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top