perl dynamic path given to 'use lib'

Posted by Ed Hyer on Stack Overflow See other posts from Stack Overflow or by Ed Hyer
Published on 2010-12-30T23:06:25Z Indexed on 2011/01/02 6:54 UTC
Read the original article Hit count: 416

So, my code (Perl scripts and Perl modules) sits in a tree like this:

 trunk/  
   util/  
   process/  
   scripts/  

The 'util' directory has, well, utilities, that things in the 'process/' dir need. They get access like this:

use FindBin;
use lib "$FindBin::Bin/../util";
use UtilityModule qw(all);

That construct doesn't care where you start, as long as you're at the same level in the tree as "util/".
But I decided that 'scripts/' was getting too crowded, so I created

   scripts/scripts1  
   scripts/scripts2  

Now I see that this doesn't work. If I run a script 'trunk/scripts/scripts1/call_script.pl', and it calls '/trunk/process/process_script.pl', then 'process_script.pl' will fail trying to get the routines from UtilityModule(), because the path that FindBin returns is the path of the top-level calling script.

The first ten ways I thought of to solve this all involved something like:

use lib $path_that_came_from_elsewhere;

but that seems to be something Perl doesn't like to do, except via that FindBin trick.

I tried some things involving BEGIN{} blocks, but i don't really know what I'm doing there, and will likely just end up refactoring. But if someone has some clever insight into this type of problem, this would be a good chance to earn some points!

© Stack Overflow or respective owner

Related posts about perl

Related posts about shared-libraries