Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a perl module find its own path?

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/progs 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?

like image 213
Clinton Avatar asked Nov 26 '12 01:11

Clinton


People also ask

How do I find my perl path in Linux?

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.

How do Perl modules work?

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.

How do I print a path in perl?

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.


2 Answers

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.

like image 139
Borodin Avatar answered Sep 20 '22 21:09

Borodin


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.

like image 25
plusplus Avatar answered Sep 19 '22 21:09

plusplus