[Canberra-pm] how to tell if a library is available?
Kim Holburn
kim at holburn.net
Mon Jun 16 10:30:46 PDT 2008
OK, thanks, that's good but I have a problem. Maybe this is a dumb
question but if I test for this and in one branch of the test I invoke
the library with a use statement it works if the library is present
but not if it's not there:
eval "use Text::Autoformat" ;
if (!$@) {
use Text::Autoformat ;
....
Can't locate Text/Autoformat.pm in @INC (@INC contains: ...
) at script.pl line 316.
BEGIN failed--compilation aborted at script.pl line 316.
Am I mistaken here? Do I need to have a "use" statement in addition
to the eval?
If I don't have the use statement I get lots of other errors (using -w).
Kim
On 2008/Jun/14, at 11:40 AM, Jacinta Richardson wrote:
> Kim Holburn wrote:
>> I have a script which could use a library (Text::Autoformat) if
>> it's available in the current libs or if it's in the directory the
>> script is in. I'd like a simple test to tell this before I either
>> try and use it or use a work-around.
>> Is there any simple test for this?
>
> Try using it with a string eval and then check $@. For example:
>
> eval "use Foo;";
> if($@) {
> print "Could not find Foo\n";
> }
>
> eval "use Bar;";
> if($@) {
> print "Could not find Bar: $@\n";
> }
>
> print "Program completed\n";
>
>
> with Foo.pm being:
>
> package Foo;
>
> print "Foo!\n";
>
> 1;
>
> yields:
>
> jarich at teddybear:/tmp$ perl test.pl
> Foo!
> Could not find Bar: Can't locate Bar.pm in @INC (@INC contains:
> /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8
> /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8
> /usr/share/perl/5.8 /usr/local/lib/site_perl
> /usr/local/lib/perl/5.8.4 /usr/local/share/perl/5.8.4 .) at
> (eval 2) line 1.
> BEGIN failed--compilation aborted at (eval 2) line 1.
>
> Program completed
>
> If you choose to try to use a second module upon failing the first,
> remember to save $@ if you ever intend to use it:
>
> eval "use Foo;";
> if(my $E = $@) {
> print "Could not find Foo, trying Bar\n";
> eval "use Bar;";
> # $@ has now changed!
> }
>
> I hope this helps.
>
> J
--
Kim Holburn
IT Network & Security Consultant
Ph: +39 06 855 4294 M: +39 3494957443
mailto:kim at holburn.net aim://kimholburn
skype://kholburn - PGP Public Key on request
Democracy imposed from without is the severest form of tyranny.
-- Lloyd Biggle, Jr. Analog, Apr 1961
More information about the Canberra-pm
mailing list