Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Conditional compilation

Reply
Thread Tools

Conditional compilation

 
 
Wcool
Guest
Posts: n/a
 
      03-06-2008
Hi,

From the chapter on internal workings of Perl in the Perl book, I
think it should be possible to conditionally compile something based
on a variable.

I have the following real problem: my code has a lot of statements
that conditionally print a tracing message.
That is based on a command line parameter.

Is it possible during the compilation phase to NOT generate any code
for those statements if that command line is not set.
Example:


sub Trace
{
my ($debug, $msg) = @_;

if ($debug) {
print $msg."\n";
}
}

sub SomeFunc1
{
Trace($opt_d, 'In SomeFunc1');

....


Trace($opt_d, . 'some_var = '.$var1);

...

Trace($opt_d, . 'some_var2 = '.$var2);

etc
}

If opt_d is not set I like the Perl not to generate any code for it
internally (some sort of skip)
The compiler would go through SomeFunc1 at just don't parse the Trace
statement.
This would increase speed of the program (slightly).

Is this possible?

Thanks,

Jeroen
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-06-2008

Quoth Wcool <(E-Mail Removed)>:
>
> From the chapter on internal workings of Perl in the Perl book, I
> think it should be possible to conditionally compile something based
> on a variable.
>
> I have the following real problem: my code has a lot of statements
> that conditionally print a tracing message.
> That is based on a command line parameter.
>
> Is it possible during the compilation phase to NOT generate any code
> for those statements if that command line is not set.
> Example:
>
>
> sub Trace
> {
> my ($debug, $msg) = @_;
>
> if ($debug) {
> print $msg."\n";
> }
> }
>
> sub SomeFunc1
> {
> Trace($opt_d, 'In SomeFunc1');
>
> ....
>
>
> Trace($opt_d, . 'some_var = '.$var1);
>
> ...
>
> Trace($opt_d, . 'some_var2 = '.$var2);
>
> etc
> }
>
> If opt_d is not set I like the Perl not to generate any code for it
> internally (some sort of skip)
> The compiler would go through SomeFunc1 at just don't parse the Trace
> statement.
> This would increase speed of the program (slightly).


The closest to what you are asking for (without getting into seriously
difficult stuff) is to something like this:

use constant DEBUG => $opt_d;

use Carp;

sub Trace {
my ($msg) = @_;
carp $msg; # carp will send the message to STDERR, and
# report it from the point of view of your
# caller.
}

sub SomeFunc1 {
Trace('in SomeFunc1') if DEBUG;
}

Perl will optimize out the Trace calls at compile time, since the
condition is a compile-time constant.

HOWEVER, there is a problem here: the value of $opt_d has to be known at
compile time. This means that your getopt call (or whatever) will have
to be in a BEGIN block.

Also note that the $opt_x interface to Getopt::Std (assuming that's what
you're using) is a bad idea. It's much better to pass getopt a hash to
put the options in.

Ben

 
Reply With Quote
 
 
 
 
Wcool
Guest
Posts: n/a
 
      03-10-2008
Thank you both!

Jeroen
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional compilation in VHDL avishay VHDL 4 08-01-2005 05:57 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Conditional Compilation Directives in aspx file? Praveen Ramesh ASP .Net 2 04-13-2005 03:28 AM
Conditional Compilation Directives in aspx file? Praveen ASP .Net 0 04-12-2005 02:50 PM
Preprocessor conditional compilation variable not being saved Chris P ASP .Net 0 10-28-2003 08:48 PM



Advertisments