(crazyinsomniac) Re: Checking to see if a particular Module is installed
by crazyinsomniac (Prior) on Aug 11, 2000 at 06:36 UTC
|
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·.¸_____|
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ | [reply] |
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();
| [reply] [d/l] [select] |
|
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 | [reply] [d/l] |
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") | [reply] [d/l] [select] |
|
my $got_GD = 0;
eval { require GD };
unless ($@) {
$got_GD = 1;
GD->import();
}
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |
|
| [reply] [d/l] |
|
|
|
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 | [reply] [d/l] |
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 | [reply] |
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!
| [reply] |
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. | [reply] |
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. | [reply] [d/l] |
|
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")
| [reply] |