[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

for a Perl script to find a module



This is an OO Perl question.  If you know more
about Perl's `@INC' than I do, please reply.

Problem: in the `debram' source, I have a perl
helper script which I can only successfully run
if my $PWD happens to be the same directory the
script resides in.  In other words, I cannot

  $ helper/myscript

Rather, I must

  $ cd helper
  $ ./myscript

This is inconvenient, is ugly, and makes it hard
to use the script in a general way.

The complication is that the script `use's a .pm
module which I wrote myself.  The module lives
in the same directory with the script.  The
reason the script refuses to run from the wrong
$PWD is that it cannot find the module.

The script and module together need to be
relocatable.  That is, if I move `myscript'
and `MyModule.pm' together from `helper/'
to `newhelper/', then I want

  $ newhelper/myscript

automatically to find `MyModule.pm' right there
in `newhelper/'.  I do not want to modify the
script every time I move it; I expect the script
just to find the module.  The module is right
there in the script's own directory.  For the
script to find the module there should not be
hard.

(The script and the module are development
helpers.  Thus they are found in the source
only, not in any binary the source builds.)

That is the problem.  After a fair bit of
experimentation, here is my kludge of a solution
thus far:

  BEGIN {
    my $dir = `readlink -f $0`;
    chomp $dir;
    $dir =~ s/^(.*)\/.*?$/$1/ or $dir = '.';
    unshift @INC, $dir;
  }
  use MyModule;

Does this kludge work?  Yes, it does seem to
work, but it's a kludge, and it's not right.  It
marshals a BEGIN block and a foreign utility
program to trick perl into doing something
simple and straightforward.  Even if it were not
ugly, it is sly and I cannot trust it.  How can
I fix it?  All I want the script to do is to
find a module.  The module is not sneaking
around, hiding somewhere, after all; it stands
right there at the script's shoulder, ready to
serve.

-- 
Thaddeus H. Black
P.O. Box 1020, Christiansburg, Virginia 24068
USA; +1 540 961 0920, t@b-tk.org

Attachment: pgpJtMoygsz0y.pgp
Description: PGP signature


Reply to: