hmm just a quick remark:

Calling use will also execute the import function which might do unexpected things.

You might prefer require (or even do ) which are just part of use.

(mnemonic use > require > do > eval with > read as "extends")

Furthermore you should probably check the error code to avoid false negatives.

There are many reasons why a code execution might fail.

Saying so, a function to check @INC without eval-ing the located code would be safer°. (especially if you don't want to activate the whole dependency chain with consecutive use calls)

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

update

°) The page for require has sample code showing how @INC is searched, just skip the part after do($realfilename); to avoid compilation and add the logic for ref($prefix) if needed. (alternatively see also these modules)

use Carp 'croak'; use version; sub require { my ($filename) = @_; if ( my $version = eval { version->parse($filename) } ) { if ( $version > $^V ) { my $vn = $version->normal; croak "Perl $vn required--this is only $^V, stopped +"; } return 1; } if (exists $INC{$filename}) { return 1 if $INC{$filename}; croak "Compilation failed in require"; } foreach $prefix (@INC) { if (ref($prefix)) { #... do other stuff - see text below .... } # (see text below about possible appending of .pmc # suffix to $filename) my $realfilename = "$prefix/$filename"; next if ! -e $realfilename || -d _ || -b _; $INC{$filename} = $realfilename; my $result = do($realfilename); # but run in caller's namespace if (!defined $result) { $INC{$filename} = undef; croak $@ ? "$@Compilation failed in require" : "Can't locate $filename: $!\n"; } if (!$result) { delete $INC{$filename}; croak "$filename did not return true value"; } $! = 0; return $result; } croak "Can't locate $filename in \@INC ..."; }

You might want to use this as a first step and doing your eval only if it's not found in a second step (in order to play safe). Like this you will only execute module code if the parsing mechanism fails (which shouldn't be ever the case and deserves a warning)

NB: This approach answers the question "Can a module be found in @INC" which is slightly different to "Is a module distribution installed" since:


In reply to Re: find module name from the module archive (in @INC) (updated) by LanX
in thread find module name from the module archive by Lotus1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.