I've used File::Basename for a similar purpose--I think __FILE__ is probably more appropriate than $0, though. I'm guessing that either
use lib dirname __FILE__;
or the push @INC variant of the same thing would work.
I don't think $0 is appropriate because if memory serves it only contains the base name of the file if the script is invoked that way (for instance if it's in the user's PATH somewhere). Though I could be totally wrong about that :-)
If God had meant us to fly, he would *never* have given us the railroads.
--Michael Flanders | [reply] [d/l] |
ChemBoy already noted that $0 contains the name of the
script, not the name of the current file.
But he didn't point out that including "." includes the
current working directory in @INC, which has no connection
with the location of the file your code is in. Furthermore
including "." explicitly is redundant - Perl includes "."
in @INC already.
The information about variables is covered, of course, in
perlvar. The __FILE__ literal in perldata. And the
knowledge of what directory "." is is covered in any
introduction to filesystems. | [reply] |
Well, the question itself did ask for the path to the script... not the current file. Or did I read it wrong?
Regardles... I was just posting something that had worked for me when I needed a similar problem solved.
If what I said was completely wrong, though, thank you for pointing it out.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
------END GEEK CODE BLOCK------
Translate
"Weird things happen, get used to it."
Flame ~ Lead Programmer: GMS
| [reply] |
No, '.' is already on the @INC list. That will look in the current working directory, which is not the same as the directory containing the script.
I agree that File::Basename::fileparse will be a little simpler in this case than File::Spec::splitpath, because it doesn't separate the volume out. So it becomes
use lib { my ($base,$path)= File::Basename::fileparse ($0); $path };
Assuming it's OK to leave off the @suffix parameter. The dirname function in that module looks like a natural, except for the enigmatic comment at the bottom: "For example, for the input file specification lib/, fileparse() considers the directory name to be lib/, while dirname() considers the directory name to be ." Huh? If I don't understand what it's doing, I can't use it.
And I still wonder if $0 contains the actual path, or just what was on the command line.
—John | [reply] [d/l] |