-1

We have a Perl framework in which we have created some customized module. Whenever I am trying to use the modules inside a sub-directory it throw Error.

Below is my Folder structure, frameworkfolder being the base/root directory of the project:

.../frameworkfolder/PerlModules/Initialize.pm

I need to use this Initialize.pm in a Perl file which is in another directory

.../frameworkfolder/TestCases/FVT/fvt/fvt.pl

In fvt.pl, when I use PerlModules::Initialize, it throws the following error:

Can't locate PerlModules/Initialize.pm in @INC (you may need to install the PerlModules::Initialize module) (@INC contains: /usr/opt/perl5/lib/site_perl/5.28.1/aix-thread-multi /usr/opt/perl5/lib/site_perl/5.28.1 /usr/opt/perl5/lib/5.28.1/aix-thread-multi /usr/opt/perl5/lib/5.28.1)

How do I use the Initialize.pm Perl package from fvt.pl? or any other perl files inside any other directory?

I have tried below solutions but no luck:

use lib '../../../frameworkfolder';

and

use FindBin; 
use lib "$FindBin::Bin"

and

use FindBin;
use File::Spec;
use lib File::Spec->catdir($FindBin::Bin, '../../', 'lib');

and

#!/usr/local/bin/perl -w -I /frameworkfolder/

Did not find answer in : How is Perl's @INC constructed? (aka What are all the ways of affecting where Perl modules are searched for?)

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • `use lib '/full/path/to/dir/with/Module';` **has to** work if it's all correct. You can shorten it (using `FindBin`) but get it working first. Or set up `PERL5LIB` correctly. Note that the `package` declaration in the module needs to be written, and the module `use`d, accordingly; see [this post](https://stackoverflow.com/a/54433246/4653379). In short: If the module has `package Dir::Mod;` then you need `use Dir::Mod;` and the `use lib '...';` needs to have the path that contains `Dir` (not `Mod.pm`!) – zdim Apr 18 '20 at 17:39
  • Please edit the question and post: (1) Exact `package` declaration from the module; (2) Exact `use` statement from programs that use it (3) Exact location of the module on the system. – zdim Apr 18 '20 at 17:40

1 Answers1

1

If PerlModules/Initialize.pm contains package Initialize;:

use FindBin qw( $RealBin );
use lib "$FindBin::RealBin/../../../PerlModules";
use Initialize;

If PerlModules/Initialize.pm contains package PerlModules::Initialize;:

use FindBin qw( $RealBin );
use lib "$FindBin::RealBin/../../..";
use PerlModules::Initialize;

(The former makes a lot more sense.)

Notes:

  • $Bin breaks if a symlink to the script is used. $RealBin avoids this problem.
  • No point in using File::Spec. Your module isn't going to be used on VMS or Classic Mac OS.
  • I have no idea why you think of the of paths you used (../../../frameworkfolder, ../../lib and /frameworkfolder/) would work. None of those are remotely close. Have someone teach you the basics of relative paths.
ikegami
  • 367,544
  • 15
  • 269
  • 518