Search code examples
perlrequire

perl `require` for file given by variable


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"

Solution

  • 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.