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

I have a snippet like this

BEGIN { require "call_simulator.pm" ; use constant { START => 1000 , NO_OF_CALLS => 1 , DEBUG => 1 , DET_DEBUG => 1 , TOTAL_TIME => 1 , DETAILED_TIME => 1 , RESPONSE => 1 , SLEEP => undef , MICRO_SEC => 1 , }; # This works well if ( MICRO_SEC ) { eval "use Time::HiRes qw (time )" ; } # This works # use if ( MICRO_SEC ) , "Time::HiRes" ; # This won't work # use if ( MICRO_SEC ) , "Time::HiRes qw(time)" ; # ERROR : # Can't locate Time/HiRes qw(time).pm in @INC (@INC contains: /etc/ +perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib +/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/ +local/lib/site_perl .) at /usr/share/perl/5.10/if.pm line 13. }
I just wanted to include the Time::HiRes module with a time
function at a compile time with a condition
Using eval statement its possible !
but my question is how can I achieve this with the "use if" to include a module with a function !

Replies are listed 'Best First'.
Re: Include module at compile time with condition
by Corion (Patriarch) on May 07, 2011 at 07:12 UTC
Re: Include module at compile time with condition
by Eliya (Vicar) on May 07, 2011 at 07:11 UTC
    use if ( MICRO_SEC ) , "Time::HiRes", qw(time) ;

      Thats what im looking for
      Thanks a lot
      I made a slight change ( of course both are same ) for the coding style

      use if ( MICRO_SEC ) , Time::HiRes => qw(time) ;

        Note that the fat comma doesn't work here under use strict (i.e., you'd still need to quote the module name):

        #!/usr/bin/perl use strict; use if ( 1 ) , Time::HiRes => qw(time) ;
        $ ./903506.pl Bareword "Time::HiRes" not allowed while "strict subs" in use at ./903 +506.pl line 3. Execution of ./903506.pl aborted due to compilation errors.
Re: Include module at compile time with condition
by John M. Dlugosz (Monsignor) on May 07, 2011 at 07:11 UTC
    Your answer is given by the first example in the Synopses for the if module.

Re: Include module at compile time with condition
by Khen1950fx (Canon) on May 07, 2011 at 07:59 UTC
    I tried it like this---the fat comma worked in this case because I added a left curlie and a right curlie to use constant. Without the curlies, MICRO_SEC becomes a bareword:
    #!/usr/bin/perl use strict; use warnings; use constant ({ START => 1000, NO_OF_CALLS => 1, DEBUG => 1, DET_DEBUG => 1, TOTAL_TIME => 1, DETAILED_TIME => 1, RESPONSE => 1, SLEEP => undef, MICRO_SEC => 1, }); BEGIN { eval { require "call_simulator.pm"; use if MICRO_SEC, 'Time::HiRes' => qw(time); } }
Re: Include module at compile time with condition
by Nikhil Jain (Monk) on May 07, 2011 at 09:09 UTC

    Some general remark:

    "use Readonly" - Facility for creating read-only scalars, arrays, hashes. instead of "use constant"

    It would be nice to see this section COMPARISON WITH "use constant" in Readonly

Re: Include module at compile time with condition
by wind (Priest) on May 07, 2011 at 17:20 UTC

    Yet another slightly different way to accomplish this. Note the BEGIN is redundant

    use constant ({ START => 1000, NO_OF_CALLS => 1, DEBUG => 1, DET_DEBUG => 1, TOTAL_TIME => 1, DETAILED_TIME => 1, RESPONSE => 1, SLEEP => undef, MICRO_SEC => 1, }); use if MICRO_SEC, qw(Time::HiRes time);

    And before learning about the if package, I'd do the following:

    BEGIN { if (MICRO_SEC) { require Time::HiRes; Time::HiRes->import('time'); } }