1753819 Members
8783 Online
108805 Solutions
New Discussion юеВ

Perl: Current Directory

 
SOLVED
Go to solution
Kalin Evtimov
Regular Advisor

Perl: Current Directory

Hi!
I was using a piece of code to find out the curent dir of the script:

use Cwd;
my $dir = getcwd; #current directory

I found something special: When the script , let's say in /tmp/scripts has been run by another script in /tmp, then $dir contains the current directory of the "main" script (/tmp), and not the one I need (/tmp/scripts).

Does anybody know another function that works properly?
6 REPLIES 6
Muthukumar_5
Honored Contributor
Solution

Re: Perl: Current Directory

You are not chaning any directory right? If you are moving physically from one directory to another directory with cd then PWD variable will be changed. Cwd is updated on that. Else, executiong patch will be same so that it will show /tmp only.

In child script change directory with cd then print getcwd information. It will show /tmp/scripts locations.

hth.
Easy to suggest when don't know about the problem!
Orhan Biyiklioglu
Respected Contributor

Re: Perl: Current Directory

This is the proper functionality indeed. If you invoke it from a script running under /tmp the actual current working directory will definitely be /tmp. cwd means the working directory not where the directory that the script resides.

You can also see this behaviour if you go in a different directory and invoke the script using the full path

ie.

#cd /
#/tmp/scripts/myscript


then cwd will be / not /tmp/scripts

hth
Arunvijai_4
Honored Contributor

Re: Perl: Current Directory

There is no discrepancy in this, its working properly, Check out here, http://perl.active-venture.com/lib/Cwd.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Kalin Evtimov
Regular Advisor

Re: Perl: Current Directory

Yeah, I know ot is working properly, but I need the directory where the child script is. When the whole thing is run by cron, then it looks even worse, because my $dir is / :(
Imagine, I want to put the whole stuff on another server and the main script is not in /tmp, but in /usr. This is why I cannot use absolute names. I need for each script to find out where it resides and put that in $dir
Arunvijai_4
Honored Contributor

Re: Perl: Current Directory

Have you tried with use FindBin; ??

use FindBin;

to find the directory where the perl script resides.

http://perl.org.il/pipermail/perl/2003-January/000948.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
H.Merijn Brand (procura
Honored Contributor

Re: Perl: Current Directory

1. use Cwd; is NOT driven by the $PWD environment variable. The reason is clear: there might not be one. Not all parent shells define it (think csh)

2. check the scripts for chdir () calls. The script, whoever/whatever called it, can be in one single location at the time, so if a parent script (sh, perl, C, ...) does a cd/chdir, the child script shares that location

3. If you need to know the location of the running script, you can indeed use FindBin, but you can also examine $0 and/or $^X

Demo:

lt09:/tmp 110 > cat test.sh
#!/bin/sh

pwd

echo none
perl -l test.pl

echo relative
perl -l ./test.pl

echo absolute
perl -l /tmp/test.pl

echo chdir
cd /usr
pwd

echo none
test.pl

echo relative
../tmp/test.pl

echo absolute
/tmp/test.pl

echo extend PATH
export PATH=$PATH":/tmp"

echo none
test.pl

echo relative
../tmp/test.pl

echo absolute
/tmp/test.pl
lt09:/tmp 111 > cat test.pl
#!/pro/bin/perl

use warnings;
use strict;

use Cwd;

my $pwd = getcwd;
print "\$0: $0, \$^X: $^X, pwd: $pwd\n";
lt09:/tmp 112 > sh test.sh
/tmp
none
$0: test.pl, $^X: /pro/bin/perl, pwd: /tmp

relative
$0: ./test.pl, $^X: /pro/bin/perl, pwd: /tmp

absolute
$0: /tmp/test.pl, $^X: /pro/bin/perl, pwd: /tmp

chdir
/usr
none
test.sh: line 19: test.pl: command not found
relative
$0: ../tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
absolute
$0: /tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
extend PATH
none
$0: /tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
relative
$0: ../tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
absolute
$0: /tmp/test.pl, $^X: /pro/bin/perl, pwd: /usr
lt09:/tmp 113 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn