I'm creating my own module, lets call it X::Y
. Of course, the module will be in the file X/Y.pm
.
Lets say Y
needs to call an external program, prog
. Ideally I'd just like to put prog
in X
, so I can run X/prog
. I'd like to not have hardcode X/prog
s full path, and for the module to work regardless of the current working directory set.
How can I find the full path of a module from inside a module? Or is there a better way to do this?
Normally, perl will be in your shell's path. It can often be found lurking in /usr/bin or /usr/local/bin. Use your system's find or locate command to track down perl if it doesn't appear in your command path.
The perl1 compiler (yes, there is a compiler although it's interpreted language) loads files, compiles them, then switches to run time to run the compiled code. When it encounters a new file to load, it switches back to compile time, compiles the new code, and so on. To load a module at compile time, you use it.
use Cwd qw(abs_path); my $path = abs_path($0); print "$path\n"; But it is displaying the filename of my script along with the directory.
The full path to the source file currently being executed is supplied by Perl's __FILE__
special literal.
However I would prefer to see the external program installed where it would normally be, and the path there either coded as a constant in the Perl code or included in the PATH
environment variable.
Borodin answered the question but some related information:
FindBin - finds the directory that the script was run from (use within the script itself or within a package loaded by it)
Neil Bower's CPAN modules for getting a module's path - detailed review of modules for finding another module's path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With