module locating

I

ivowel

dear perl experts:

assume main.pl uses module module.pm, and both sit in /home/users/dir/.
I know I can add directories to library paths, but I don't like to
hardcode such paths---especially, because I may later want to do "mv
/home/users/dir /home/users/dirnew" and still be able to execute from
another location call "$ perl /home/users/dirnew/main.pl" . my first
attempt was

(my $libd= $0) =~ s/(.*\/).*/$1/g;
use lib "$libd";
die "$0 $libd\n";

tells me about an empty compile time value. Is there a recommended way
to accomplish this?

sincerely,

/iaw
 
X

xhoster

dear perl experts:

assume main.pl uses module module.pm, and both sit in /home/users/dir/.
I know I can add directories to library paths, but I don't like to
hardcode such paths---especially, because I may later want to do "mv
/home/users/dir /home/users/dirnew" and still be able to execute from
another location call "$ perl /home/users/dirnew/main.pl" . my first
attempt was

(my $libd= $0) =~ s/(.*\/).*/$1/g;
use lib "$libd";
die "$0 $libd\n";

tells me about an empty compile time value. Is there a recommended way
to accomplish this?

You need a strategically placed BEGIN block to get the variable defined
at compile time before the use lib is compiled.

my $libd;
BEGIN {($libd= $0) =~ s/(.*\/).*/$1/g;}
use lib "$libd";

I'm not sure how this method is different from FindBin, which you may
also want to look in to.


Xho
 
B

Ben Morrow

Quoth Sherm Pendley said:
$0 is just the filename of your script, not the full path.

This is not true. $0 usually contains the path the script was invoked as
(it depends on the OS exactly what get put in argv[0]), and FindBin uses
$0 to do its job.

Ben
 
M

Michele Dondi

You need a strategically placed BEGIN block to get the variable defined
at compile time before the use lib is compiled.

my $libd;
BEGIN {($libd= $0) =~ s/(.*\/).*/$1/g;}
use lib "$libd";
^^^^^^^
^^^^^^^

Incidentally, useless use of quotes.
I'm not sure how this method is different from FindBin, which you may
also want to look in to.

Well, some respected Perl hackers think FindBin has shortcomings, and
that there are more reliable alternatives along the lines of the OP's
one. But the issue is controversial:

http://perlmonks.org/?node_id=41213


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top