[Canberra-pm] how to tell if a library is available?

George Bills george.s.bills at gmail.com
Mon Jun 16 20:07:07 PDT 2008


Hi Kim,

Text::Autoformat should already have been used in the eval, so you
don't need to use it again inside the "if not eval error" block. I'm a
little hazy here, so maybe someone should correct me, but I think that
code there will try and "use Text::Autoformat" regardless of what
happens in the eval - because that use statement is evaluated at
compile time, before any other code (like the eval and the if
statement).

On Tue, Jun 17, 2008 at 3:30 AM, Kim Holburn <kim at holburn.net> wrote:
> 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
>
>
>
> _______________________________________________
> Canberra-pm mailing list
> Canberra-pm at pm.org
> http://mail.pm.org/mailman/listinfo/canberra-pm
>


More information about the Canberra-pm mailing list