RandyRiegel
Programmer
How can I get the directory the script resides in from within the script?
I tried using the following:
But if I run it from another directory it returns the directory I'm in not the one the script is in. Example: if I'm in my home directory and run my perl script which resides in /usr/local/bin it uses /home/randy for current directory.
The reason I need to do this is because my script creats a "log" file in the same directory as the script. If I run the script from another directory it creates the log file wherever I'm at not in the directory where the script is. I don't want the path hardcoded because I want to be able to copy the script to another directory without changing it.
I tried using the following:
Code:
use Cwd;
my $path = cwd;
print("$path\n");
But if I run it from another directory it returns the directory I'm in not the one the script is in. Example: if I'm in my home directory and run my perl script which resides in /usr/local/bin it uses /home/randy for current directory.
The reason I need to do this is because my script creats a "log" file in the same directory as the script. If I run the script from another directory it creates the log file wherever I'm at not in the directory where the script is. I don't want the path hardcoded because I want to be able to copy the script to another directory without changing it.