Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: using 'use' conditionally

by blssu (Pilgrim)
on Nov 05, 2002 at 16:59 UTC ( #210516=note: print w/replies, xml ) Need Help??

in reply to using 'use' conditionally

You can't always replace 'use' with 'require' because they work slightly differently. 'use' works at compile time and also imports stuff into your code. 'require' works at run time and never imports stuff.

This example isn't exactly like your problem, but I think it might be useful anyways.

BEGIN { if ($^O eq 'MSWin32') { eval "use APDM::ConfigWin32"; print $@ if ($@); } else { eval "use APDM::ConfigUnix"; print $@ if ($@); } }

Replies are listed 'Best First'.
Re: Re: using 'use' conditionally
by BUU (Prior) on Nov 06, 2002 at 02:09 UTC
    If you care that much about importing from the module:
    if($opt_g) { require Tk; Tk->import; }
    Difference goes away! wow.
      Difference goes away! wow.

      BUU is right. Additionally, blssu, you don't need to import if you're going to use the OO interface of whichever module you required... because good OO interfaces don't export their methods.

      setenv EXINIT 'set noai ts=2'

      Sure, that's pretty close to what 'use' does now, but the 'eval' helps future-proof your code. The hand-rolled import isn't clever, it's just extra work. Laziness is one of the three pillars...

      The common efficiency and security disadvantages of 'eval' aren't issues here because (1) run-time 'require' must do an 'eval' eventually anyway, and (2) the 'eval' uses a string constant that is easy to understand and verify.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://210516]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2021-10-17 19:02 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (72 votes). Check out past polls.