How can you get current script directory in Perl?
This has to work even if the script is imported from another script (require).
This is not the current directory
Example:
#/aaa/foo.pl
require "../bbb/foo.pl"
#/bbb/bar.pl
# I want to obtain my directory (`/bbb/`)
print($mydir)
The script foo.pl could be executed in any ways and from any directory, like perl /aaa/foo.pl, or ./foo.pl.
Sorry for the other 4 responses but none of them worked, here is a solution that really works.
In below example that adds the
libdirectory to include path the$dirnamewill contain the path to the current script. This will work even if this script is included usingrequirefrom another directory.