in reply to Re: module location
in thread module location

This is driving me nuts - I don't understand why this doesn't work:
#!/usr/bin/perl -w use strict; my $perl_module; BEGIN { my $ct_args = @ARGV; if ($ct_args != 1) { print "\nUsage: pwhich_short <perl module name>\n\n"; print "Example: pwhich_short DBI\n\n"; exit; } $perl_module = shift; }; eval "use $perl_module" or die "Can't find $perl_module: $!\n";
This code gets this error:
Can't find DBI:
That's it. Does this work for you? Any ideas?

Thanks.

Replies are listed 'Best First'.
Re: Re: Re: module location
by liz (Monsignor) on Nov 07, 2003 at 15:50 UTC
    I think you want to replace:
    eval "use $perl_module" or die "Can't find $perl_module: $!\n";
    by:
    eval "use $perl_module"; die "Can't find $perl_module: $@\n" if $@;

    Eval just returns whatever was returned by use, which to my knowledge is undefined (please someone correct me if I'm wrong).

    You need to check whether $@ is non-empty after the eval().

    Liz

      Liz, you get the gold star! It works - I've used eval before, but mostly just in DBI transaction code where I had an eval block({}) and then checked $@ after, but I guess I forgot that for *any* eval errors you need to check $@. Thank you very much!

      Hardy