Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically add a program's library path to @INC in Perl program?

Tags:

perl

I'm working on an application that is written in Perl. The application is so large now that I want to move each class out into a separate file. This complicated building and installing the program; when it was only file it could easily be copied in place anywhere. Now I have to use a build system like Build::Module or ExtUtils::MakeMaker.

I have structured my source tree so that I have two directories: bin where the program launcher is, and lib where a number of modules are. Bin contains an executable Perl script which the user invokes, and it loads the necessary module from the lib directory.

The problem that I have is that I want the user to be able to specify a prefix where they want the program installed, similar to the --prefix option offered by packages based on GNU Autotools. Since this might not be a standard path where Perl looks for modules (for example /opt/program) the user will see a message saying something like Can't locate Program.pm in @INC.

Is there a way to make the program detect where the modules should be loaded frm and dynamically add that path to @INC? I don't want the user to have to manually work with environment variables like PERL5LIB in order to get the program running.

like image 970
drupsspen Avatar asked Jan 30 '26 15:01

drupsspen


1 Answers

Couldn't you use findbin and lib ?:

use FindBin qw($Bin);
use lib "$Bin/lib";
like image 173
salparadise Avatar answered Feb 02 '26 04:02

salparadise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!