/home/directory/lib
-- Libary2.pm
/home/directory/project
-- file1.pl
-- file2.pl
-- Libary1.pm
In `file1.pl`, we can include both libraries by,
-- Libary2.pm
/home/directory/project
-- file1.pl
-- file2.pl
-- Libary1.pm
use lib '/home/directory/project';
use lib '/home/directory/lib';
or
use lib '/home/directory/lib';
So in `file1.pl`,
#include both directory at once,
use lib qw(
/home/directory/project
/home/directory/lib
);
use Libary1;
use Libary2;
`file2.pl`,will have the same
use lib qw(
/home/directory/project
/home/directory/lib
);
use Libary1;
use Libary2;
use lib qw(
/home/directory/project
/home/directory/lib
);
use Libary1;
use Libary2;
The above code is good for 1 or 2 files. But if there are 10+ perl files, we have to include the same lib path in all files. If the library path changes, we had to change all files with the correct path name.
So what I did was to have 1 module `config/LibPaths.pm` that will have the paths, and all files will include the module.
/home/directory/project
/home/directory/lib
);
use Libary1;
use Libary2;
lib
-- Libary2.pm
project
-- file1.pl
-- file2.pl
-- Libary1.pm
config
-- LibPaths.pm
In `LibPaths.pm`
-- Libary2.pm
project
-- file1.pl
-- file2.pl
-- Libary1.pm
config
-- LibPaths.pm
package LibPaths;
use lib qw(
/home/directory/project
/home/directory/lib
);
and I include the `LibPaths.pm` module In `file1.pl` as follows
use lib qw(
/home/directory/project
/home/directory/lib
);
use File::Basename;
use Cwd qw(abs_path);
use lib dirname (abs_path(__FILE__)) . "/config";
use LibPaths;
- `use Cwd qw(abs_path)` is needed to get the current file. use Cwd qw(abs_path);
use lib dirname (abs_path(__FILE__)) . "/config";
use LibPaths;
- `use lib dirname (abs_path(__FILE__))` will get the directory name of the current file
- `use lib dirname (abs_path(__FILE__)) . "/config";` will include the config folder. I liked to put the `LibPaths.pm` under a config directory and my project has 50+ files. But If the module is in same directory, including config is not needed.
**So now when the library paths changes, I can update in 1 place which is `LibPaths.pm` **
There are other Perl modules like “FindBin” which will serve this purpose, But I didnt want to introduce a new dependency in my project. So I followed the above hack. I might be an ugly hack, but it works :D