Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Only

by rrwo (Friar)
on Dec 11, 2000 at 22:18 UTC ( [id://46105]=perlquestion: print w/replies, xml ) Need Help??

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

I want to use Time::HiRes qw(time) only if it is available, but otherwise fallback to plain old time.

What's the best way to:

  1. Check if a module exists, and if it does, import its subroutines
  2. I'm unable to redefine the internal time routine, but somehow Exporter and Time::HiRes are able to...

Replies are listed 'Best First'.
Re: Only
by arturo (Vicar) on Dec 11, 2000 at 22:37 UTC

    You don't need to overload an inbuilt function IF you define one of the same name and call it with the ampersand prepended, i.e. &time will execute a user-defined subroutine named time if it exists. Otherwise, you can check out dchetlin's tutorial Overloading '=' for some info on how to overload inbuilt functions.

    As to the first problem: you can wrap require Time::Hires in an eval inside a BEGIN block, and if that eval fails, print a warning (if appropriate) and set a flag that tells your program to use the internally defined routine. If the eval succeeds, call import Time::Hires and you'll have the symbols imported.

    #!/usr/bin/perl -w use strict; use vars '$internal'; BEGIN: { eval "require Time::Hires"; if ($@) { # if it failed print "Can't load Time::Hires, falling back to own routines\ +n"; $internal =1; } else { import Time::Hires; $internal = 0; } }
    HTH,

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

(Ovid - try eval)Re: Only
by Ovid (Cardinal) on Dec 11, 2000 at 22:27 UTC
    I would use something like the following:
    if ( eval { require Time::Hires } ) { # The require was successful } else { # It was unsuccessful }
    The eval with braces will trap the die if Time::Hires is not on your system.

    Cheers,
    Ovid

    Update: Whups! Since I hadn't used Time::Hires before, I wasn't aware of what it does internally. I suspect that autark's correction below is appropriate.

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      But the function time() from Time::HiRes will not be imported. To make this binding between &main::time and &Time::HiRes::time we have to this at compile time:
      BEGIN { eval { require Time::HiRes; import Time::HiRes qw(time) }; if($@) { warn "Unable to import Time::HiRes\n" }; }
      Without the BEGIN, the binding of time to the correct function would not happen:
      eval { require Time::HiRes; import Time::HiRes qw(time) }; print time()
      Would produce f.exs: 976556706. While
      BEGIN { eval { require Time::HiRes; import Time::HiRes qw(time) }; } print time()
      Would produce the correct time using the Time::HiRes::time function: 976556789.394509.

      Autark.

Re: Only
by InfiniteSilence (Curate) on Dec 11, 2000 at 23:32 UTC
    I think I have another weird way to do this:

    #!/usr/bin/perl -w
    use AUTOLOADER;
    sub AUTOLOAD {
     $v = $AUTOLOAD;
     $v =~ s/.*:://g;
     if($v eq "SomeFunctionFromHires")
     {
            #if this is called, it didn't work
            require qw(c:\temp\hires.pm);
     }
    }
    
    &SomeFunctionFromHires();
    &foo;
    &SomeOtherFunctionFromHires();
    &secondfoo;
    1;
    
    Here's what is in the phony Hires.pm:
    
    sub foo
    {
    print "foo!";
    }
    
    sub secondfoo
    {
    print "secondfoo!";
    }
    
    1;
    

    Celebrate Intellectual Diversity

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found