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

Checking to see if a particular Module is installed

by skazat (Chaplain)
on Aug 11, 2000 at 06:19 UTC ( [id://27443]=perlquestion: print w/replies, xml ) Need Help??

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

hello all, i'm making two scripts that are used for log analysis, one makes on the fly tables of data, on using the GD library, produces images based on the information of the first script,

(the first script outputs HMTL, and could also output a tag that says: <img src=graph_script.cgi?var1=$one&var_two=$two> and so on)

this is for distribution, i was wondering if there is a way to see if certain Modules are installed, like the GD module, and then act accordingly. It would be simple to just not write that img tag if its not there.

something like:

 

if($got_GC == 1) { 

#how graph
}else{ 
#say "hey, you don't gots the gear yo, stick with plain text"


}   

-justin simoni
!skazat!
  • Comment on Checking to see if a particular Module is installed

Replies are listed 'Best First'.
(crazyinsomniac) Re: Checking to see if a particular Module is installed
by crazyinsomniac (Prior) on Aug 11, 2000 at 06:36 UTC
    Check out this link: http://www.perl.com/pub/doc/manual/html/lib/ExtUtils/Installed.html.
       use ExtUtils::Installed;
       my ($inst) = ExtUtils::Installed->new();
       my (@modules) = $inst->modules();
       my (@missing) = $inst->validate("DBI");
       my $all_files = $inst->files("DBI");
       my $files_below_usr_local = $inst->files("DBI", "all", "/usr/local");
       my $all_dirs = $inst->directories("DBI");
       my $dirs_below_usr_local = $inst->directory_tree("DBI", "prog");
       my $packlist = $inst->packlist("DBI");
    
    - above info retrieved from www.perl.com
    ---------------------------------------------------
    from shell : perl -MFoo -we 1
    where Foo is module name, to find out if Foo is installed
    
    -got from a chat session / which turned into a post
    
    Update: punkkid is right, but he forgot ';' This'll work just as well: if(eval("require CGI;")) { print "CGI is here";} ______________________________________________ |_____¸.·ooO--(> cRaZy is co01. <)--Ooo·.¸_____| ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
RE: Checking to see if a particular Module is installed
by athomason (Curate) on Aug 11, 2000 at 07:11 UTC
    punkkid's answer is close, but doesn't quite do it if the use statement doesn't return a true value. For example, perl -e "eval 'use CGI' or die" dies. The correct way would be
    my $package = "GD"; eval "use $package; 1" ? gd_stuff() : alternate_stuff();
      Slightly simpler:
      eval 'require GD'; if ($@) { # problems with GD, fall back to non-GD } else { # we have GD, go for it! }
      There's no point in using use, because you can't affect the compile-time imports at runtime if it's not there!

      -- Randal L. Schwartz, Perl hacker

RE: Checking to see if a particular Module is installed
by tye (Sage) on Aug 11, 2000 at 16:52 UTC

    The slowness of string eval (and headache of no compile-time catching of typos inside the eval) is not required here since we have constant code. I'd use:

    if( my $got_GD= eval { require GD } ) { GD->import(); }

    Note that the import is done at run time (not compile time as would be the case with a use not inside eval), so you can't access your imported routines using barewords (you have to type the parentheses -- and, yes, I'm intentionally ignoring the use of &).

    I often like to access imported (or predeclared) routines using barewords because it catches typos at compile time instead of only when that particular typo is executed (writing full coverage test suites is time consuming). But there are no good ways to do that with conditional importing (there are several ways, just none of them are good).

            - tye (but my friends call me "Tye")
      But this still doesn't provide the prototypes of GD subroutines for later parsing, so even though the aliasing is done, we lose the argument context.

      Caveat executor!

      One other problem: your $got_GD is now local to the if, so you won't be able to use it later. So let's rewrite that as:

      my $got_GD = 0; eval { require GD }; unless ($@) { $got_GD = 1; GD->import(); }

      -- Randal L. Schwartz, Perl hacker

        Good call on the problem with placement of my. I don't see your other point.

                - tye (but my friends call me "Tye")
Re: Checking to see if a particular Module is installed
by elusion (Curate) on Aug 11, 2000 at 07:00 UTC
    How about if you perform a use function inside of an eval, like this: if (eval("use GD")) { } else { } This will execute the eval statement as a seperate perl program and if works it will return true.

    - p u n k k i d
    "Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

Re: Checking to see if a particular Module is installed
by Maclir (Curate) on Aug 11, 2000 at 08:39 UTC
    Check out Perl Diver. It will show all the modules you have loaded.

    Ken

RE: Checking to see if a particular Module is installed
by tilly (Archbishop) on Aug 11, 2000 at 18:19 UTC
    My preference is not widely shared. I take a page from Linus Torvalds, define the API to program to, and write a simple wrapper for that API. Then all I ever need to do is write to that wrapper.

    I might implement in this case by checking for the existence of the module at run-time, and initialize a bunch of anon subs accordingly. My API wind up behind the scene calling them, and they will automagically do the right thing (even if that thing is to spit out debugging info or else just do nothing).

    For the full philosophy, here is a good rant from Linus explaining the why's and wherefore's. Even if you disagree it is a worthwhile read.

Re: Checking to see if a particular Module is installed
by skazat (Chaplain) on Aug 12, 2000 at 07:21 UTC

    thanks alot everyone, that was a good discussion. I checked the code i was using, and i was actually using the GifGraph module, which is supposed to be a wrapper for GD now, but when i tried looking for the GD module, it was no where in site :)

    nevertheless, this is a good thing to know, and probably one of those things that if you messed up and found a "good" (not best) answer, interesting errors will creep up.

    -justin simoni
    !skazat!

Re: Checking to see if a particular Module is installed
by jimt (Chaplain) on Aug 11, 2000 at 21:58 UTC
    My approach has always been:
    BEGIN { eval "use GD"; $can_GD = 1 unless $@; };
    Or something along those lines. Almost indistinguishable from the previous posts, admittedly, but I always try to be careful to wrap it up in BEGIN blocks. That way you're sure that the import is actually done at compile time instead of run time and it's virtually indistinguishable from an actual use.

    So you don't need to worry about functions not be prototyped or whatnot.

      Actually, you probably still need to worry about that (be sure to test your code without the GD module installed). Most of the things I find useful about doing the import at compile time can't be easily used if you want to support the possible lack of the module. See my other nodes in this thread for more info.

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://27443]
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: (5)
As of 2024-04-18 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found