Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Conditional including of a module

by edwardo_ (Initiate)
on May 14, 2009 at 01:51 UTC ( #763972=perlquestion: print w/ replies, xml ) Need Help??
edwardo_ has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,
need to use excel modules only if a user actually requested for that via command line switch..
On some hosts excel isn't installed, hence just putting "use Win32::OLE" and friends results in error whenever the script is invoked with whatever arguments, and I'd like to supress this.
So I asked on CB and someone advised to do the following:
use if ($excel_requested), "Win32::OLE";

which has worked in a way - it stopped producing complaints on a hosts without excel; however then it died on hosts with excel installed, complaining that it's not.

Any other suggestions?

Versions: v5.10.0, WinXP various SPs (the one with excel) and Win Server EE 2003 SP2 (the one without).
On the host without excel the script is ran as .exe, produced by PAR Packager 0.982 .

Thanks for your time..

Comment on Conditional including of a module
Download Code
Re: Conditional including of a module
by roubi (Hermit) on May 14, 2009 at 02:21 UTC
    Have you tried this?
    if ($excel_requested){ require Win32::OLE; }
      Thanks for all who responded, apologies, could not posted earlier.
      The above suggestion worked only when I removed requirement for Win32::OLE::Const 'Microsoft Excel' , which I used earlier and thought I need.
      Apparently in this case writing to excel worked without that addition, when earlier for some reason it couldn't.
      Thanks again.
Re: Conditional including of a module
by lakshmananindia (Chaplain) on May 14, 2009 at 02:42 UTC

    You can suppress the error by using eval

    eval 'use Win32::OLE'; if($@) { #Do nothing. }
    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


Re: Conditional including of a module
by doug (Pilgrim) on May 14, 2009 at 19:39 UTC

    I've done this several times for several different reasons. Your main issue is that the use statement is interpreted as soon as the ; is hit. What I've done has been a variation of

    if ( $need_library ) { eval "use THE_LIBRARY_IN_QUESTION;"; die $@ if ( $@ ); }

    or something close to that. I think I once populated a list with needed libraries and looped through the list to load (use) them.

    -- doug

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-09-20 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (157 votes), past polls