Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: disable functions if module not installed

by almut (Canon)
on Nov 03, 2008 at 22:25 UTC ( #721229=note: print w/ replies, xml ) Need Help??


in reply to disable functions if module not installed

Something like this works for me:

our $is_magick; BEGIN { $is_magick = eval "require Image::Magick" ? 1 : 0; }

(my $is_magick; is fine, too, btw)

Note that the assignment in our $is_magick = 1; in your snippet would be executed after the BEGIN block...  Also, the BEGIN block is only necessary if you need $is_magick's state to be valid early on.


Comment on Re: disable functions if module not installed
Select or Download Code
Re^2: disable functions if module not installed
by Anonymous Monk on Nov 04, 2008 at 12:42 UTC
    require returns true on success, and string-eval eats memory :)
    BEGIN { $is_magick = eval {require Image::Magick;}; } C:\>perl -e"print require CGI;" 1 C:\>perl -e"print require CGIshamalamadingdong;" Can't locate CGIshamalamadingdong.pm in @INC
      require returns true on success,

      yes, and false (undef) otherwise. That was the idea :)

      string-eval eats memory

      Why would that be (in this case, where the string is just a few bytes)?  Any tests/data to prove the claim?

      C:\>perl -e"print require CGI;" 1 C:\>perl -e"print require CGIshamalamadingdong;" Can't locate CGIshamalamadingdong.pm in @INC

      Not sure what this is meant to demonstrate — I thought we were talking about eval "require ...":

      $ perl -E '$ok = eval "require CGI" ? 1:0; say $ok' 1 $ perl -E '$ok = eval "require CGIshamalamadingdong" ? 1:0; say $ok' 0

      (note: swap single and double quotes if you're on Windows)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2014-04-24 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls