Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't "use lib" take effect in this way?

Tags:

perl

In my app, I put all the modules in one directory , let's just call it libx. Since it's up to the user to choose where to deploy the app, I don't want to hardcode the lib path.

So at the beginning of myapp.pl, I wrote the following lines of code.

#! /usr/bin/perl -w
use strict;

my $curr_dir = $0;
my $curr_lib = $curr_dir;
$curr_lib =~ s/myapp\.pl/libx/;

use $curr_lib ;

Instead of getting what I'm expecting, I got compiling errors!

So what's wrong with my code? I don't want to hardcode the lib path when using use lib, how should I do this?


Sorry I forgot to mention that when the app is deployed, myapp.pl and libx are in the same directory.

like image 256
Haiyuan Zhang Avatar asked Aug 18 '09 05:08

Haiyuan Zhang


1 Answers

use happens at compile-time, not run-time, so your variable hasn't been set yet. You can do:

my $curr_lib;
BEGIN {
    $curr_lib = $0;
    $curr_lib =~ s/myapp\.pl/libx/;
}    
use lib $curr_lib;

or you could:

use FindBin;
use lib "$FindBin::Bin/libx";
like image 72
ysth Avatar answered Nov 03 '22 04:11

ysth