PERL – How to Get the File Path For The Running PERL Script

Here is some sample code for determining what the file path is to the PERL script containing the code:

use Cwd qw(realpath);
my $fullpath = realpath($0);

If you just do $0 alone in the second line, that only gives you the filename that was executed. For example, if you include the first line of code above in a PERL script file named “perl” $0 will return “”. But if you ran the PERL script using an absolute path like this “perl /user/local/”, $0 would return “/user/local/”. So the example above uses the realpath() function to guarantee the full absolute path is always returned.


Leave a Reply