Reputation: 8931
I want to use a module that the path to the file will be in a variable.
I tried using this code:
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
my ($library, $zipped, $aid_class_file);
GetOptions ("aid_class_file=s" => \$aid_class_file,
"res_lib=s" => \$library,
"zip" => \$zipped);
require $aid_class_file;
but it doesn't work. How do I do it?
edit: The error message is:
Can't locate Error.pm in @INC (@INC contains: /usr/lib/perl5/5.8.3/x86_64-linux-thread-multi /usr/lib/perl5/5.8.3 /usr/lib/perl5/site_perl/5.8.3/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.3/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl .) at /nfs/iil/disks/home10/imelam2/learn_flow/flow_to_change/AidClass.pm line 6.
BEGIN failed--compilation aborted at /nfs/iil/disks/home10/imelam2/learn_flow/flow_to_change/AidClass.pm line 6.
Compilation failed in require at statistics.pl line 11.
I want to add the file called AidClass.pm
and not Error.pm
I ran using the line:
statistics.pl -aid_class_file="/nfs/iil/disks/home10/imelam2/learn_flow/flow_to_change/AidClass.pm"
Upvotes: 3
Views: 919
Reputation: 19528
Your error demonstrates that the AidClass cannot find the Error.pm file which causes it to crash. The Error.pm is called at line 6 of the AidClass.pm:
Can't locate Error.pm
It is looking for it on the following path:
/usr/lib/perl5/5.8.3/x86_64-linux-thread-multi
/usr/lib/perl5/5.8.3 /usr/lib/perl5/site_perl/5.8.3/x86_64-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.3/x86_64-linux-thread-multi
/usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl
Depending on how your AidClass is you could add its directory as a lib in the AidClass.pm so it will look for the Error.pm in there as well:
use lib '/nfs/iil/disks/home10/imelam2/learn_flow/flow_to_change/';
use Error;
Or you could do that from the script you are calling AidClass and instead of calling use Error;
call use AidClass;
and if the Error.pm is within the folder it will work just fine.
Upvotes: 3
Reputation: 69224
You don't say in what way it "doesn't work", so this is all a bit of a guess.
Have you tried reading the documentation? In particular the bit that talks about loading modules using a variable.
If EXPR is a bareword, the require assumes a ".pm" extension and replaces "::" with "/" in the filename for you, to make it easy to load standard modules. This form of loading of modules does not risk altering your namespace.
In other words, if you try this:
require Foo::Bar; # a splendid bareword
The require function will actually look for the "Foo/Bar.pm" file in the directories specified in the @INC array.
But if you try this:
$class = 'Foo::Bar'; require $class; # $class is not a bareword #or require "Foo::Bar"; # not a bareword because of the ""
The require function will look for the "Foo::Bar" file in the @INC array and will complain about not finding "Foo::Bar" there. In this case you can do:
eval "require $class";
Upvotes: 3