So, I'm trying to import a module into a Perl package and I try to make it in the cleanest way possible. First, I read some of the previous threads (like: link).
The module is located in the following path: /p/disk/tools/perl/5.26.1/lib64/site_perl. The module is Data::TreeDumper.
I used the FindBin module in order to import it:
BEGIN {
use Exporter ();
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
use FindBin;
use lib "$FindBin::RealBin";
use lib "$FindBin::Bin";
use lib "$FindBin::Bin/../lib";
use lib "/p/disk/tools/perl/5.26.1/lib64/site_perl";
@ISA = qw(Exporter);
@EXPORT_OK = qw(# ALL subs
);
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}
use constants qw(:all);
use Data::TreeDumper;
It works for me, but the path is hardcoded. I would like to include the path into the constants package (my package). But it kind of a circle because in order to use the constants (and the path) I need to import it first, but then, from where? huh.
So I thought to remove the use lib "/p/disk/tools/perl/5.26.1/lib64/site_perl"; line and somehow import first the constants file and then do something like:
use constants qw(:all);
use lib $PATH_TO_THE_MODULE; # represents path to the module area
use Data::TreeDumper;
But It does not work for. Is it possible to achieve it?
Before trying to import the module (didn't know it was installed in a global place), I installed it locally and it worked. But I prefer not to keep the non-mine modules in the same directory as the project. So IT guys installed the module in a global area and I just asking if it is possible to import it in a clean way.
EDIT: I'm trying to insert the path /p/disk/tools/perl/5.26.1/lib64/site_perl into the constants file in a global variable (let's call it $PATH_TO_THE_MODULE). Then I want to import from the other package the constants package so I use FindBin. And Then I want to import the module using the $PATH_TO_THE_MODULE variable which points to the area of the module. So it should look something like this:
use constants qw(:all);
use lib $PATH_TO_THE_MODULE; # represents a path to the module area
use Data::TreeDumper;
But it does not work. I tried to switch to require because I understood that require will use dynamically. But it also fails with Can't locate Data/TreeDumper.pm in @INC.
So if we connect both pieces of code we get:
package test;
$|=1;
use strict;
use warnings;
BEGIN {
use Exporter ();
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
use FindBin;
use lib "$FindBin::RealBin";
use lib "$FindBin::Bin";
use lib "$FindBin::Bin/../lib";
@ISA = qw(Exporter);
@EXPORT_OK = qw(# ALL subs
);
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}
use constants qw(:all);
use lib $PATH_TO_THE_MODULE; # represents a path to the module area
use Data::TreeDumper;
print "Hi\n";
constants package:
package constants;
$|=1;
use strict;
use warnings;
BEGIN {
use Exporter ();
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
use FindBin;
use lib "$FindBin::RealBin";
use lib "$FindBin::Bin";
use lib "$FindBin::Bin/../lib";
@ISA = qw(Exporter);
@EXPORT_OK = qw($PATH_TO_THE_MODULE);
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}
our $PATH_TO_THE_MODULE = "/p/disk/tools/perl/5.26.1/lib64/site_perl";
If I'll remove use lib $PATH_TO_THE_MODULE; and use Data::TreeDumper; from the test package and I'll print $PATH_TO_THE_MODULE, it will compile and print the path as expected.