Click here to get back home

Loading Perl Modules from same directory as script

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Loading Perl Modules from same directory as script Donald Gillies 02-13-2006
Posted by Donald Gillies on February 13, 2006, 5:35 pm
Please log in for more thread options


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 $"`;
}
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

Posted by A. Sinan Unur on February 13, 2006, 6:51 pm
Please log in for more thread options


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

--
(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


Posted by Paul Lalli on February 14, 2006, 12:39 pm
Please log in for more thread options


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


Posted by Donald Gillies on February 14, 2006, 2:40 pm
Please log in for more thread options



>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

Posted by Paul Lalli on February 14, 2006, 3:59 pm
Please log in for more thread options


Donald Gillies wrote:
>
> >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


Similar ThreadsPosted
Loading Perl Modules from same directory as script February 13, 2006, 5:27 pm
Loading Perl Modules faster January 21, 2005, 3:26 am
New Script Directory needs your scripts listed September 3, 2005, 6:32 pm
Newbie: Bundling Perl script and modules in a Mac OS X app? May 17, 2005, 5:44 pm
Modules not installing to standard LIB directory November 17, 2005, 8:14 am
Perl Module to search in a directory and sub-directories December 25, 2006, 2:05 pm
Module loading order February 12, 2006, 11:42 pm
LWP fail to catch loading balacing websites March 9, 2005, 6:10 am
Loading (from Internet), resizing and saving images March 22, 2005, 6:15 am
AppConfig loading undef values into ARGCOUNT_LIST March 22, 2007, 2:54 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap