Conditional compilation

Discussion in 'Perl Misc' started by Wcool, Mar 6, 2008.

  1. Wcool

    Wcool Guest

    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
     
    Wcool, Mar 6, 2008
    #1
    1. Advertising

  2. Wcool

    Ben Morrow Guest

    Quoth Wcool <>:
    >
    > 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
     
    Ben Morrow, Mar 6, 2008
    #2
    1. Advertising

  3. Wcool

    Wcool Guest

    Thank you both!

    Jeroen
     
    Wcool, Mar 10, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris P
    Replies:
    0
    Views:
    441
    Chris P
    Oct 28, 2003
  2. avishay

    Conditional compilation in VHDL

    avishay, Aug 1, 2005, in forum: VHDL
    Replies:
    4
    Views:
    3,023
    Andy Peters
    Aug 1, 2005
  3. Praveen
    Replies:
    0
    Views:
    363
    Praveen
    Apr 12, 2005
  4. Praveen Ramesh
    Replies:
    2
    Views:
    2,175
    Steven Cheng[MSFT]
    Apr 13, 2005
  5. Alec S.
    Replies:
    10
    Views:
    10,166
    Alec S.
    Apr 16, 2005
Loading...

Share This Page