Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Path of current executing script/module?

Status
Not open for further replies.

FireAndGlass

Programmer
Jun 5, 2006
4
US
I'm trying to retrieve the full path of the current executing script or module. I am aware that $FindBin::Bin will return the path of the toplevel executing module, but that is different. getcwd() will get the current working directory, but again, that can be different.

The situation I'm using it in is this. I have a perl script that is loading a module in another directory. The Module then needs to load another module, but it has to dynamically load a certain module from different paths based on certain conditions.

so basically i have this:

[tt]~/foo/MyScript.pl
~/bar/MyModule.pm
~/bar/a/MyOtherModule.pm
~/bar/b/MyOtherModule.pm[/tt]

in MyScript.pl:

[tt]use lib "~/bar";
use MyModule;[/tt]

in MyModule it will determine if directory a or b is the correct path, then add that to the libs path:

[tt]use $scriptingLib; // "a" or "b"
use MyOtherModule;[/tt]

However it is unable to find it. Upon failing to load, it says @INC will have "{ a /usr/nathan/bar . }"

I've been told that if one path is relative to another in @INC, upon a use call that it will search that relative path too. This can't be the case, otherwise it would find MyModule in /usr/nathan/bar/a.

I've scoured search engines and forums looking for an answer and can't seem to find one, yet this seems like a basic piece of information needed. Suggestions anyone? If it matters, I'm using ActivePerl 5.8g in a Cywin Bash shell.
 
Have you tried lib?

Code:
use lib $scriptingLib;
use MyOtherModule;

Are the directories /foo/ and /bar/ in the same place? You'd have to specify a path to the actual lib.

From what you've posted, having "a" in @INC would refer to "~/foo/a/" rather than "~/bar/a/" like you want.

Code:
# relative path if /bar/ and /foo/ have the same parent
use lib "../bar/$scriptingLib"; # or

# absolute path
use lib "/home/bar/$scriptingLib"; # *nix
use lib "C:/home/bar/$scriptingLib"; # win32
 
Whoops, I am using lib like you said - I mistyped it.

Code:
use lib $scriptingLib;
use MyOtherModule;

The problem is this module needs to be flexible in that a perl script from anywhere can call it and it can load it's other modules. So I would not like to rely on any knowledge about the relationship of "foo" (the original script's directory) and "bar" (the directory of MyModule.pm)

I'm still very surprised there's no simple straighforward way to get "where am i?" ie the script or module's directory.
 
$0 is just the string on the command line that invoked the script - so again it would just point you to the original perl script's directory, not the module.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top