# your_project/bin/somewhere/deep/down/script.pl use strict; use warnings; # look up from the file's location until we find a directory containing a directory # named 'lib'. Add this dir to @INC use File::FindLocalLib qw(lib); # look up until we find a dir that contains both 'lib' and 'foo', add both to @INC use File::FindLocalLib qw(lib foo); # look up until we find 'lib' and 'local'. Add 'lib' to @INC, load 'local' via local::lib use File::FindLocalLib qw(lib local::lib=local); # based on the dir we found earlier, go up one dir and try to add # 'Your-OtherModule/lib' and 'Dark-PAN/lib' to @INC File::FindLocalLib->load_extra(Your-OtherModule Dark-PAN);
I'm usually using a setup like this:
. ├── AProject │ ├── bin │ │ ├── db │ │ │ └── init.pl │ │ ├── onetime │ │ │ ├── fixup │ │ │ │ └── RT666_fix_up_fubared_data.pl │ │ │ └── import_data.pl │ │ └── web.psgi │ ├── lib │ └── local ├── MyHelperStuff │ └── lib └── CoolLib-NotYetOnCPAN └── lib
AProject, which is the actual code I'm working on. There is also probably
BProject, e.g. another microservice for the same customer.
A-Project has it's own code in
lib and it's CPAN dependencies in
local (managed via
Carton and used via
local::lib). There are a bunch of scripts / "binaries" in
bin, in a lot of different directories of varying depth.
I have some generic helper code I use in several projects in
MyHelperStuff/lib. It will never go to CPAN. I have some other code in
CoolLib-NotYetOnCPAN/lib (but it might end up on CPAN if I ever get to clean it up...)
File::FindLocalLib makes it easy to add all these paths to
@INC so I can use the code.
In each script, I just have to say:
use File::FindLocalLib qw(lib local::lib=local);
lib is added to the beginning of
local is loaded via
local::lib, without me having to know how deep in
bin the current script is located.
I can also add
to get my other code pushed to
@INC. (Though currently I put this line, and some other setup code like initialising
AProject::Run, and just