Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

eval array of required modules

by halfcountplus (Hermit)
on Apr 02, 2014 at 09:13 UTC ( [id://1080706]=perlquestion: print w/replies, xml ) Need Help??

halfcountplus has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to do this (the examples are core modules just to make the point):

no strict; my @required = ( File::Basename, File::Copy, File::Find ); use strict; my @missing; foreach (@required) { eval { require }; push @missing, $_ if $@; }

But it does not work ("Can't locate File::Basename..." etc). What's the problem and is there any way around it?

Replies are listed 'Best First'.
Re: eval array of required modules
by BrowserUk (Patriarch) on Apr 02, 2014 at 09:26 UTC

    Try it this way:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; my @required = qw( File::Basename File::Copy File::Find ); pp \%INC; my @missing; foreach (@required) { s[::][/]g; s[$][.pm]; eval { require }; push @missing, $_ if $@; } print for @missing; pp \%INC; __END__ C:\test>junk39 { "Data/Dump.pm" => "c:/perl64/lib/Data/Dump.pm +", "Exporter.pm" => "c:/perl64/lib/Exporter.pm" +, "List/Util.pm" => "c:/perl64/site/lib/List/Ut +il.pm", "Scalar/Util.pm" => "c:/perl64/site/lib/Scalar/ +Util.pm", "XSLoader.pm" => "c:/perl64/lib/XSLoader.pm" +, "c:/perl64/site/lib/sitecustomize.pl" => "c:/perl64/site/lib/sitecus +tomize.pl", "overload.pm" => "c:/perl64/lib/overload.pm" +, "strict.pm" => "c:/perl64/lib/strict.pm", "subs.pm" => "c:/perl64/lib/subs.pm", "vars.pm" => "c:/perl64/lib/vars.pm", "warnings.pm" => "c:/perl64/lib/warnings.pm" +, "warnings/register.pm" => "c:/perl64/lib/warnings/reg +ister.pm", } { "Config.pm" => "c:/perl64/lib/Config.pm", "Cwd.pm" => "c:/perl64/site/lib/Cwd.pm" +, "Data/Dump.pm" => "c:/perl64/lib/Data/Dump.pm +", "Exporter.pm" => "c:/perl64/lib/Exporter.pm" +, "File/Basename.pm" => "c:/perl64/lib/File/Basenam +e.pm", "File/Copy.pm" => "c:/perl64/lib/File/Copy.pm +", "File/Find.pm" => "c:/perl64/lib/File/Find.pm +", "File/Spec.pm" => "c:/perl64/site/lib/File/Sp +ec.pm", "File/Spec/Unix.pm" => "c:/perl64/site/lib/File/Sp +ec/Unix.pm", "File/Spec/Win32.pm" => "c:/perl64/site/lib/File/Sp +ec/Win32.pm", "List/Util.pm" => "c:/perl64/site/lib/List/Ut +il.pm", "Scalar/Util.pm" => "c:/perl64/site/lib/Scalar/ +Util.pm", "XSLoader.pm" => "c:/perl64/lib/XSLoader.pm" +, "c:/perl64/site/lib/sitecustomize.pl" => "c:/perl64/site/lib/sitecus +tomize.pl", "overload.pm" => "c:/perl64/lib/overload.pm" +, "re.pm" => "c:/perl64/lib/re.pm", "strict.pm" => "c:/perl64/lib/strict.pm", "subs.pm" => "c:/perl64/lib/subs.pm", "vars.pm" => "c:/perl64/lib/vars.pm", "warnings.pm" => "c:/perl64/lib/warnings.pm" +, "warnings/register.pm" => "c:/perl64/lib/warnings/reg +ister.pm", }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: eval array of required modules ( Module::Load
by Anonymous Monk on Apr 02, 2014 at 09:32 UTC
Re: eval array of required modules
by farang (Chaplain) on Apr 02, 2014 at 21:15 UTC

    One solution is an eval expression with explicit $_

    #!/usr/bin/env perl -l use strict; use warnings; my @required = qw( File::Basename File::Copy File::Find ); my @missing; foreach (@required) { eval "require $_"; push @missing, $_ if $@; } print "missing---> @missing" if @missing; print for sort grep m{^File/[BCF]} => keys %INC; __END__ File/Basename.pm File/Copy.pm File/Find.pm

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1080706]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-29 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found