Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How do you use the if pragma?

by qazwart (Scribe)
on Apr 08, 2013 at 16:04 UTC ( #1027544=perlquestion: print w/ replies, xml ) Need Help??
qazwart has asked for the wisdom of the Perl Monks concerning the following question:

For years, I've noticed that there's an use if pragma in Perl. How would I use it?

The Perldoc on it is sort of sparse. What is CONDITION, and how would I set it? Since this is a pragma, it's executed before anything in my code, so I can't use variables or constants as a condition:

use if CONST, Foo::bar;

Is the if pragma used, and if so, how?

Comment on How do you use the if pragma?
Select or Download Code
Re: How do you use the if pragma?
by hdb (Prior) on Apr 08, 2013 at 16:25 UTC

    Proposal to find an answer: write a script using File::Find to iterate over @INC to see whether "use if" is used anywhere and to what purpose. ;)

    UPDATE: Here is the script.

    use strict; use warnings; use File::Find; find( sub { my $f = $File::Find::name; return unless $f =~ /\.p(l|m)$/; return unless open my $fh, "<", $f; while(<$fh>){ print "$f: $_" if /^\s*use if/; } close $fh; }, @INC );

    Just one hit on my machine. "use if" seems to be unpopular.

Re: How do you use the if pragma?
by Anonymous Monk on Apr 08, 2013 at 20:55 UTC
Re: How do you use the if pragma?
by LanX (Canon) on Apr 08, 2013 at 21:07 UTC
    > How would I use it?

    how to use super search to find code:

    search 'use if' with single quote as string separator (click search !!!)

    > Since this is a pragma, it's executed before anything in my code, so I can't use variables or constants as a condition:

    constants
    Wrong, since use constant is a pragma itself, and of course order matters.

    see this example of defining a constant VERBOSE to control following use statements.

    --> Re: Redefining Imported Subs: of scope and no

    update: variables

    But TIMTOWTDI, see BEGIN for a dynamic way to define variables at start up.

    use strict; use warnings; my $VERBOSE; BEGIN {$VERBOSE=1 } use if $VERBOSE, feature => "say"; say "bla";
    update: functions

    or even better define a sub, functions are evaluated at first compilation pass.

    The following code will die in one out of two cases!

    (just in case if you wanna leave your employer a hard to debug problem =)

    use strict; use warnings; sub VERBOSE { int rand 2} use if VERBOSE, feature => "say"; say "bla";

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: How do you use the if pragma?
by tobyink (Abbot) on Apr 08, 2013 at 23:27 UTC

    My most frequent uses of if seem to be:

    # Loading either "mro" or "MRO::Compat" depending # on Perl version... # use if $] >= 5.010, 'mro'; use if $] < 5.010, 'MRO::Compat'; # Load UNIVERSAL::DOES for Perl prior to 5.10. # use if $] < 5.010, 'UNIVERSAL::DOES';

    It's pretty easy to emulate it anyway:

    # Loading either "mro" or "MRO::Compat" depending # on Perl version... # BEGIN { if ($] >= 5.010) { require mro; mro->import } else { require MRO::Compat; MRO::Compat->import } }; # Load UNIVERSAL::DOES for Perl prior to 5.10. # BEGIN { require 'UNIVERSAL::DOES' if $] < 5.010; };

    CPAN also has unless.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027544]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-12-17 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (36 votes), past polls