Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check module availablility

Status
Not open for further replies.

billbose

Programmer
Jan 15, 2001
66
US

Determine OS and then adding module
#############
#!/usr/bin/perl
use POSIX qw(uname);
my $kernel = (uname)[0];

if ( ! $kernel =~ m/^Windows/) {
use Sun::Solaris::Kstat;

}
##############

i want to use a module by making sure its available first. Can somebody tell how to do that.

I am getting an error doing that as above
---------- Capture Output ----------
> "D:\Perl\bin\perl.exe" filename.pl
Can't locate Sun/Solaris/Kstat.pm in @INC (@INC contains: D:/Perl/lib D:/Perl/site/lib .) at ups3.pl line 13.
BEGIN failed--compilation aborted at ups3.pl line 13.

> Terminated with exit code 2.



 
What does $kernel actually return?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
it returns the OS name.
Thanks for the help.
I already found the answer in the perl cookbook.
 
Here is an example from the Perl CookBook that might help you:

Code:
12.2. Trapping Errors in require or use
Problem

You need to load in a module that might not be present on your system. This normally results in a fatal exception. You want to detect and trap these failures.
Solution

Wrap the require or use in an eval, and wrap the eval in a BEGIN block:

# no import
BEGIN {
    unless (eval "require $mod") {
        warn "couldn't load $mod: $@";
    }
}

# imports into current package
BEGIN {
    unless (eval "use $mod") {
        warn "couldn't load $mod: $@";
    }
}
 
hehehe... I posted a few mintues too late...never mind! :)
 
I was thinking an eval as well, but can't see why the if can't cut it as well?

Any ideas?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
the "if " is checking for a differnt condition, it has nothing to do with wether the module is installed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top