Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

syntax question: conditional 'use' statement?

by Anonymous Monk
on Sep 19, 2006 at 13:38 UTC ( [id://573714]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,
Is there anything wrong with using this syntax?
my $debug = 1; use warnings if ($debug == 1); use Data::Dumper if ($debug == 1);
Thanks,
Jonathan

2006-09-20 Retitled by planetscape, as per Monastery guidelines

( keep:0 edit:30 reap:0 )

Original title: 'syntax question'

Replies are listed 'Best First'.
Re: syntax question: conditional 'use' statement?
by Fletch (Bishop) on Sep 19, 2006 at 13:41 UTC

    You want the if pragma.

    Update: Not to mention you'll have problems because the compilation phase use lines would be happening before the runtime initialization of your variable. You'd need to stick that in a BEGIN block.

Re: syntax question: conditional 'use' statement?
by hgolden (Pilgrim) on Sep 19, 2006 at 13:43 UTC
    Hey Jonathan,

    The problem is that use statements are evaluated before the program is run, so the program can't evaluate the conditional when it needs to figure out whether to use warnings and use Data::Dumper. Consider commenting out those two lines, except when you're debugging. (Or consider always using warnings).

    Hays

Re: syntax question: conditional 'use' statement?
by Tanktalus (Canon) on Sep 19, 2006 at 14:12 UTC

    This is just like the following "C" code:

    bool debug = true; // I think bool is a new C keyword... if (debug) { #include <assert.h> #include "mydebugstuff.h" }
    The difference is that in C, you can more easily tell what is compile-time and what is run-time based on that hashmark. In Perl, we have the BEGIN block, but most of our compile-time code is actually executed via use.

    You're mixing compile-time and run-time code, then, so it doesn't actually work. Someone mentioned the if pragma - but it's not really needed here.

    Simply remove the if's. There's nothing wrong with warnings being used at all times. There's also nothing really wrong with loading modules you don't need - slows down compilation, but not much. If's usage is better suited for modules that may not be there than debugging statements that do no harm if they're left in, IMO. Since Data::Dumper is always installed, it's pretty safe to always load.

    That said, if you want to avoid loading Data::Dumper, you can learn how to use require - then you will only load it when you need it - at runtime, not compile-time.

Re: syntax question: conditional 'use' statement?
by ptum (Priest) on Sep 19, 2006 at 13:41 UTC

    Personally, I tend to 'use warnings' unconditionally in my production code, not only in debug mode. If I was too lazy to stamp out all the warnings before going to production, I deserve whatever happens to me. If something later goes wrong, I like to know about it as early as possible. Just my $0.02 worth.

    Update: Blush. I didn't try it until now ... but I get a syntax error if I try to use warnings conditionally:

    #!/usr/local/bin/perl my $debug = 1; use warnings if ($debug == 1); use Data::Dumper if ($debug == 1); syntax error at ./test8.pl line 3, near "use warnings if" Execution of ./test8.pl aborted due to compilation errors.
Re: syntax question: conditional 'use' statement?
by mda2 (Hermit) on Sep 19, 2006 at 14:57 UTC
Re: syntax question: conditional 'use' statement?
by caelifer (Scribe) on Sep 19, 2006 at 15:55 UTC
    Of course you can always use require for Data::Dumper module, i.e. as in
    BEGIN { use constant DEBUG => 1; if (DEBUG) { require Data::Dumper; } }
    However, this would not work for warnings since it works during compilation time. Moreover, as a rule of thumb I would recommend to always include both use strict and use warnings in every perl program. Believe it or not, it will catch a lot of errors and typos and reduce the debugging time greatly. Even for the advance programming you can always localize the effect by temporarily switching off both warnings and strict with no warnings and no strict directives.
      You forgot to import.
      BEGIN { use constant DEBUG => 1; if (DEBUG) { require Data::Dumper; Data::Dumper->import() if Data::Dumper->can('import'); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://573714]
Approved by prasadbabu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2025-06-18 18:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.