Loading Perl Modules from same directory as script

Loading Perl Modules from same directory as script

am 13.02.2006 23:35:27 von Gillies

After only 6 hours of hacking around trying to do something that
should be obvious in perl - but isn't - i seem to have found two
solutions :

PROBLEM : Your team is developing a tree that has a directory
containing perl scripts. Any developer can check out a private and
executable copy of the tree. You want the scripts run by each
developer to reference their own private checked-out library modules,
and for the sake of simplicity, you want the library modules and the
perl scripts to live in the same directory when they are checked out.
I am using Perl v5.8.0.

SOLUTION #1 : "ALMOST PORTABLE SOLUTION" - in every script you put ...

BEGIN {
$my_exe = `which $0`;
while (substr($my_exe, length($my_exe) - 1, 1) ne "/") {
chop($my_exe); }
push @INC,$my_exe;
}
require "MyModule.pm";

SOLUTION #2 "IF ITS MORE THAN ONE LINE IN PERL THEN ITS PROBABLY
WRONG"

BEGIN { # load libraries from THIS SCRIPT'S directory
push @INC, `csh -c "set a=\\\`which $0\\\` ; echo -n \\\${a:h}"`;
}
require "MyModule.pm";

========================================

Now that I have two half-working solutions, is there a "Right" way to
solve this problem ?? I cannot believe that PERL module search paths
are relative to directory where you run the command vs. relative to
the directory where the executable is found - this makes no sense at
all, maybe its job security for taintperl to exist or for another
pointless and nonsensical reason ...

- Don Gillies
San Diego, CA

Re: Loading Perl Modules from same directory as script

am 14.02.2006 00:51:46 von 1usa

gillies@cs.ubc.ca (Donald Gillies) wrote in news:dsr1jf$k1b$1
@cascade.cs.ubc.ca:

> After only 6 hours of hacking around trying to do something that
> should be obvious in perl - but isn't - i seem to have found two
> solutions :
>
> PROBLEM : Your team is developing a tree that has a directory
> containing perl scripts. Any developer can check out a private and
> executable copy of the tree. You want the scripts run by each
> developer to reference their own private checked-out library modules,
> and for the sake of simplicity, you want the library modules and the
> perl scripts to live in the same directory when they are checked out.

....

> Now that I have two half-working solutions, is there a "Right" way to
> solve this problem ?? I cannot believe that PERL module search paths
> are relative to directory where you run the command vs. relative to
> the directory where the executable is found - this makes no sense at
> all, maybe its job security for taintperl to exist or for another
> pointless and nonsensical reason ...

Huh?

perldoc FindBin

perldoc lib

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines .html

Re: Loading Perl Modules from same directory as script

am 14.02.2006 18:39:26 von Paul Lalli

Donald Gillies wrote:
> After only 6 hours of hacking around trying to do something that
> should be obvious in perl - but isn't - i seem to have found two
> solutions :

Actually, it's painfully obvious to anyone who bothers to check the
built-in Perl FAQ before starting a 6-hour "hacking" mission.

> PROBLEM : Your team is developing a tree that has a directory
> containing perl scripts. Any developer can check out a private and
> executable copy of the tree. You want the scripts run by each
> developer to reference their own private checked-out library modules,
> and for the sake of simplicity, you want the library modules and the
> perl scripts to live in the same directory when they are checked out.

> Now that I have two half-working solutions, is there a "Right" way to
> solve this problem ??

Generally, one looks for the "RIGHT" solution *before* wasting six
hours to create a wrong solution.

> I cannot believe that PERL module search paths
> are relative to directory where you run the command vs. relative to
> the directory where the executable is found - this makes no sense at
> all,

I think it makes perfect sense, actually. Why should I, by default,
care where the program is actually living if I just want to include a
module in my current directory?

> maybe its job security for taintperl to exist or for another
> pointless and nonsensical reason ...

Ahhh, there's the mark of a good developer - when you don't understand
something, whine, bitch, moan, and insult.

For future reference to anyone reading this thread - the OP would have
saved himself six hours by a simple check of the Perl FAQ:

perldoc -q directory
. . .
How do I add the directory my program lives in to
the module/library search path?

use FindBin;
use lib "$FindBin::Bin";
use your_own_modules;


Boy, that was hard, wasn't it?

Paul Lalli

Re: Loading Perl Modules from same directory as script

am 14.02.2006 20:40:25 von Gillies

"Paul Lalli" writes:

>Donald Gillies wrote:
>> After only 6 hours of hacking around trying to do something that
>> should be obvious in perl - but isn't - i seem to have found two
>> solutions :

>Actually, it's painfully obvious to anyone who bothers to check the
>built-in Perl FAQ before starting a 6-hour "hacking" mission.

I recommend you might want to add "soc.manners" to your list of
newgroup subscriptions.

Why do you even bother to answer questions in this newsgroup ?? You
certainly are NO HELP to someone looking for answers.

True, I have only been programming professionally for 30 years, and
programming PERL intermittantly for 10 years, so you cannot expect me
to know everything. But, I spent my first of 6 hours looking in
google and on www.perl.com for an answer, and when none was
forthcoming, i wrote two solutions. Then, I spent another half hour
looking for a better solution - in google groups - and reading 2
separate PERL FAQS - before posting my question.

- Don Gillies
San Diego, CA

Re: Loading Perl Modules from same directory as script

am 14.02.2006 21:59:34 von Paul Lalli

Donald Gillies wrote:
> "Paul Lalli" writes:
>
> >Donald Gillies wrote:
> >> After only 6 hours of hacking around trying to do something that
> >> should be obvious in perl - but isn't - i seem to have found two
> >> solutions :
>
> >Actually, it's painfully obvious to anyone who bothers to check the
> >built-in Perl FAQ before starting a 6-hour "hacking" mission.
>
> I recommend you might want to add "soc.manners" to your list of
> newgroup subscriptions.

Really? Would that group teach me the manners necessary to write such
condescending and insulting tripe as:
> > > I cannot believe that PERL module search paths
> > > are relative to directory where you run the command vs. relative to
> > > the directory where the executable is found - this makes no sense at
> > > all, maybe its job security for taintperl to exist or for another
> > > pointless and nonsensical reason ...

?

> Why do you even bother to answer questions in this newsgroup ?? You
> certainly are NO HELP to someone looking for answers.

I can't see how you could possibly be the judge of that, as you were
not looking for answers, and I was not attempting to give them. You
had already found your own solutions, and had been told by two others
the "Right" solutions. I was pointing out how you wasted your own time
by not bothering to check the FAQ, and how you decided your own
inability to find the correct solution must imply a deficiency in the
language and those who wrote it.

> True, I have only been programming professionally for 30 years, and
> programming PERL intermittantly for 10 years,

And yet you never learned the proper name of the language? See:
perldoc -q difference

> so you cannot expect me to know everything.

I expect no one to know everything. I do expect people to check the
FAQ before embarking on a 6-hour coding mission to see if something has
already been done, and certainly before insulting the authors of the
language in a world-wide newsgroup.

> But, I spent my first of 6 hours looking in google

Yes, because random web searches are always better than the official
built-in documentation...

> and on www.perl.com for an answer, and when none was
> forthcoming, i wrote two solutions. Then, I spent another half hour
> looking for a better solution - in google groups - and reading 2
> separate PERL FAQS - before posting my question.

So you chose either to ignore the official Perl FAQ, or to not actually
read it but pretend to?

Paul Lalli

Re: Loading Perl Modules from same directory as script

am 14.02.2006 22:17:19 von Jim Gibson

In article , Donald Gillies
wrote:

> "Paul Lalli" writes:
>
> >Donald Gillies wrote:
> >> After only 6 hours of hacking around trying to do something that
> >> should be obvious in perl - but isn't - i seem to have found two
> >> solutions :
>
> >Actually, it's painfully obvious to anyone who bothers to check the
> >built-in Perl FAQ before starting a 6-hour "hacking" mission.
>
> I recommend you might want to add "soc.manners" to your list of
> newgroup subscriptions.
>
> Why do you even bother to answer questions in this newsgroup ?? You
> certainly are NO HELP to someone looking for answers.

Actually, Paul is one of the most helpful people posting in this
newsgroup. He did provide you with an answer, didn't he?

>
> True, I have only been programming professionally for 30 years, and
> programming PERL intermittantly for 10 years, so you cannot expect me
> to know everything. But, I spent my first of 6 hours looking in
> google and on www.perl.com for an answer, and when none was
> forthcoming, i wrote two solutions. Then, I spent another half hour
> looking for a better solution - in google groups - and reading 2
> separate PERL FAQS - before posting my question.

Your initiative and perseverance in trying to find your own solution
are to be commended. I think that it is your poor attitude that might
put people off. You seem to have blamed Perl for your difficulty in
finding the "Right" answer. After programming Perl for 10 years, you
should know that Perl's module search paths are in the @INC array and
neither relative to the directory where the command is run nor to the
directory where the executable is found. Lose the attitude and you will
find many more helpful people on this newsgroup.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com