Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script directory

Status
Not open for further replies.

RandyRiegel

Programmer
Sep 29, 2003
256
US
How can I get the directory the script resides in from within the script?

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.
 
Try:

[URL unfurl="true" said:
http://www.kichwa.com/quik_ref/spec_variables.html[/URL]]
$0 Contains the name of the file containing the Perl script being executed. Depending on your OS, it may or may not include the full pathname.

- George
 
On XP it doesn't

One option might be to read a hard wired config file which holds the program path, just a thought

--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top