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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to handle a module not able to be located 1

Status
Not open for further replies.

wardy66

Programmer
Jun 18, 2002
102
AU
Hello all

I wish to code some Perl scripts which can gracefully handle modules not being present.

If I just do "use xyz::abc" the script fails compilation. I need to be able to test if a subroutine exists (use defined, I suppose) and if not, just have a guess.

Code:
use File::Spec::Function qw(splitpath);

if ( not defined &splitpath ) {
    $path = "/tmp"
}
else {
    ($path) = splitpath( $ddd$ )
}

This sort of thing.

TIA
 
Here's a short example that does what i think you're asking for. It uses Perl's 'eval BLOCK' expression to catch any error into $@ from the expression contained in the block. You can't put use() in an eval statement so you have to use the longhand method of module calling: require() the module and then import() all the functions you want to use from it.

#!/usr/bin/perl -w

use strict;
eval {
require File::Spec::Functions;
import File::Spec::Functions qw(splitpath curdir updir);
};
warn "Error in module load: $@" if $@;

my ($ddd,$path) = ("/path/to/file", undef);

$path = eval { splitpath($ddd) };

if ($@) {
print "Error in loading function: $@";
$path = "/tmp";
}
print "PATH: $path\n";

BTW, perhaps its just a typo on your part, but the actual name of the module is File::Spec::Functions, with an 's' at the end of Function. Perhaps that's why you're getting the error you're getting?

jaa
 
Thanks justice

That's a great idea. I had forgotten about eval blocks but I think you're right in them being my saviour, in this case.

It was a typo - the reason the module isn't found is that some of my customers are using 5.004 and don't have that module included. The same script works fine on 5.6 and finds the module.

I'll get coding to allow for missing modules right away.

Is there a specific reason you eval'd the subroutine call? I have been tinkering with the problem for a while and the interim solution I came up with was to comment out the "use" statement. Then the "defined" function on the subroutine worked fine.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top