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

Hello Monks,

I'm having an issue with one of my scripts where I try and require other Perl modules via eval 'require' using the -I flag and it fails, whereas it works via another eval 'require' in another script, where the includes directories are more hardcoded into the script.

Code:

Module Validator (simple):
#====================== #BEGIN MODULE VALIDATOR #====================== #!/usr/intel/bin/perl -dw use vars qw( $TOP_MODULE $DEBUG ); use pm::Common; use package_name::module; $DEBUG=0; $TOP_MODULE="Module Validator"; &error(-1, "Inappropriate arguments specified! One, and only one, modu +le must be defined") if($#ARGV != 0); &error(-2, "Bad module syntax. Only a-z, A-Z, 0-9, '_' are allowed fo +r the name, with :: as package scoping separators") if($ARGV[0] =~ /[ +^\w:\.]/); local $@=0; eval 'require "'.$ARGV[0].'";'; if($@) { &error(1, "Failed include stage for '$ARGV[0]'. Error message:\n$@ +"); } else { # ... } print "$ARGV[0]: Valid\n"; sub error { &Common::error(@_); } #====================== #END MODULE VALIDATOR #======================
try/foo.pm:
#===================== #BEGIN FOO::TRY #===================== package foo::try; + + use pm::Common; + + + 1; #===================== #END FOO::TRY #=====================
Run / output:
filc8166[559]% perl -I`pwd` ./bin/Module_Validator.pl foo::try + + Loading DB routines from perl5db.pl version 1.27 Editor support available. + + Enter h or `h h' for help, or `man perldebug' for more help. + + DB<1> s main::((eval 7)[./bin/Module_Validator.pl:16]:1): 1: require "foo::try"; main::(./bin/Module_Validator.pl:7): 7: DB<2> c (Module Validator) ERROR: Failed include stage for 'foo::try'. Error m +essage: Can't locate foo::try in @INC (@INC contains: /nfs/fm/disks/fm_vt_n190 +29/users/gwcooper/vibe_regress /usr/intel/pkgs/perl/5.8.5/lib/5.8.5/i +686-linux-64int /usr/intel/pkgs/perl/5.8.5/lib/5.8.5 /usr/intel/pkgs/ +perl/5.8.5/lib/site_perl/5.8.5/i686-linux-64int /usr/intel/pkgs/perl/ +5.8.5/lib/site_perl/5.8.5 /usr/intel/pkgs/perl/5.8.5/lib/site_perl .) + at (eval 7)[./bin/Module_Validator.pl:16] line 1. + + Backtrace: at ./bin/Module_Validator.pl line 40 main::error(1, 'Failed include stage for \'foo::try\'. Error m +essage:\x{a}Can\'t ...') called at ./bin/Module_Validator.pl line 19 Debugged program terminated. Use q to quit or R to restart, use O inhibit_exit to avoid stopping after program termination, h q, h R or h O to get additional info. DB<2> q filc8166[560]% pwd /nfs/fm/disks/fm_vt_n19029/users/gwcooper/vibe_regress filc8166[561]% ls foo/ try.pm

Any ideas what could be wrong?

Thanks in advance!

-Garrett

Replies are listed 'Best First'.
Re: Issues with including via eval 'require'
by jettero (Monsignor) on Apr 26, 2007 at 17:31 UTC

    I think you probably don't want the extra quotes. require "filename" means to require a filename, where require File::Name means to search @INC for a class (filename of File/Name.pm).

    The usual reason behind the use of eval "require ..." is to make the contents of your variable into a bare word. The other reason, obviously, is to trap errors if the module is missing; or to execute the require at runtime instead of doing it before main execution.

    You might also consider interpolation, so you don't have to explicitly concatenate like that.

    eval "require $ARGV[0]"; die $@ if $@;

    I have assumed that "foo::try" actually is in another file. If it's in the same file, your require is never going to work. :)

    -Paul

      Ah, brilliant! That did the trick! Sometimes I get confused between "require ''" and "require", I think.. Thank you! PS foo::try was in another file :).
      require always happens at runtime.