Compiler directives for Perl?

Discussion in 'Perl Misc' started by Yash, Feb 7, 2004.

  1. Yash

    Yash Guest

    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
    Yash, Feb 7, 2004
    #1
    1. Advertising

  2. Yash

    gnari Guest

    "Yash" <> wrote in message
    news:...
    > 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
    gnari, Feb 7, 2004
    #2
    1. Advertising

  3. [posted & mailed]

    On 7 Feb 2004, Yash wrote:

    >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

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
    Jeff 'japhy' Pinyan, Feb 7, 2004
    #3
  4. Yash

    Tore Aursand Guest

    On Sat, 07 Feb 2004 05:37:26 -0800, Yash wrote:
    > 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.


    --
    Tore Aursand <>
    "Nothing is certain but death and taxes. Of the two, taxes happen
    annually." -- Joel Fox
    Tore Aursand, Feb 7, 2004
    #4
    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. DotNetWorks

    Page Directives

    DotNetWorks, Jan 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    460
    Teemu Keiski
    Jan 5, 2004
  2. Philip Townsend

    compiler directives

    Philip Townsend, Feb 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    495
    Michael Mayer [C# MVP]
    Feb 5, 2004
  3. Praveen
    Replies:
    0
    Views:
    359
    Praveen
    Apr 12, 2005
  4. Kenneth Lantrip

    help with compiler directives

    Kenneth Lantrip, Jul 18, 2004, in forum: C Programming
    Replies:
    19
    Views:
    474
    Allin Cottrell
    Jul 21, 2004
  5. Replies:
    17
    Views:
    534
    Jim Langston
    Feb 9, 2006
Loading...

Share This Page