mandog has asked for the wisdom of the Perl Monks concerning the following question:
My limited experience and brief experiments suggest that at least on LINUX, the default working directory is always the directory the Perl script is in
Givein a file: /home/me/test.txt and a script: /home/me/wd.pl
#!/usr/bin/perl -w
use strict;
use Cwd;
print "cwd() is:\t\t",cwd(),"\n";
print "should be:\t\t/home/me/\n\n";
print "\$ENV{PWD} is:\t\t",$ENV{PWD},"\n";
print "should be: \t\t/home/me/\n\n";
print '`pwd` is:',"\t\t",`pwd`;
print "should be:\t\t/home/me/\n\n";
# both work fine
open(FH,'<','test.txt') or die "$!\n";
open(FH,'<','/home/me/test.txt') or die "$!\n";
... everything works as I expect. whether I call ./wd.pl or /home/me/wd.pl
However, my searches here and on perldoc.com using google and super search have not found official confirmation of this (apparent) fact.
Clues?
email: mandog
Re: current working dir is location of script?
by Zaxo (Archbishop) on Jul 31, 2003 at 03:07 UTC
|
Not a fact. It looks like you've been in /home/me/ whenever you've run this. Try from the command line,
[me]$ mkdir /home/me/foo
[me]$ cd /home/me/foo
[foo]$ /home/me/wd.pl
After Compline, Zaxo | [reply] [d/l] |
Re: current working dir is location of script?
by The Mad Hatter (Priest) on Jul 31, 2003 at 03:12 UTC
|
It isn't a fact. When you call something from the command line, the current working directory is where ever you are in the filesystem when you call it. Save this script to your home directory (say /home/me, to use your example):
#!/usr/bin/perl
print $ENV{PWD}, "\n";
Then cd /home/me and ./testcwd. It will print
/home/me
Then cd / and /home/me/testcwd. It will print
/
| [reply] [d/l] [select] |
Re: current working dir is location of script?
by graff (Chancellor) on Jul 31, 2003 at 03:31 UTC
|
Note that you will see different behavior if you use the "chdir" function within perl -- when the perl script does a "chdir" to some other path, this does NOT alter the current value of $ENV{PWD} -- the Cwd module will provide the truth in this case, as will the use of `pwd`, and $ENV{PWD} will be wrong still refer to the path where you were when you started the script.
| [reply] [d/l] [select] |
Re: current working dir is location of script?
by edan (Curate) on Jul 31, 2003 at 07:09 UTC
|
Hi!
I think your main question has already been answered quite well, but here's a little more practical info for you in case you need to write a script that needs to read a config file from the same directory that the script is located, or something similar.
You can use the subroutine dirname() in the module File::Basename to get the script's location, using the perl special variable $0. You can then use the function chdir() to change your script's current working directory. Also, if you are want to make sure that $ENV{PWD} truly reflects your script's working directory, you can override perl's builtin chdir() with the one from the module Cwd, which will keep $ENV{PWD} up to date for you! Nifty, huh?
See the following code snippet for an example of how you might do this.
#!/usr/bin/perl -lw
use Cwd 'chdir';
use File::Basename;
print "started in $ENV{PWD}";
chdir dirname $0;
print "now in $ENV{PWD}";
-- 3dan | [reply] [d/l] |
|
# myscript.pl
use FindBin;
use lib $FindBin::Bin;
use MyModule; # kept in same directory as myscript.pl
-- [ e d @ h a l l e y . c c ] | [reply] [d/l] |
Re: current working dir is location of script?
by cleverett (Friar) on Jul 31, 2003 at 03:23 UTC
|
ceverett@ceverett:~$ cat bin/test.pl
exec 'pwd';
exit;
ceverett@ceverett:~$ perl bin/test.pl
/home/ceverett
IOW, the current working directory a program starts in is the one the program gets executed from.
HTHYO | [reply] [d/l] |
cwd, location, and the secret of -x
by wufnik (Friar) on Jul 31, 2003 at 07:53 UTC
|
just a small additional point in case you are not bored with cwd yet. and a question for perl command line flag devotees.
imagine i have a script, cwd.pl, whose location is
home/wufnik/scripts, and looks something like:
use Cwd;
print cwd . "\n";
then, as pointed out above, if i run this using:
/home/wufnik/scripts/cwd.pl, sitting in /home/,
/home will be printed as the current working directory.
exactly the same behaviour will be found if you use
the perl -S cwd.pl
method of script invocation, ie: search through my $PATH,
which naturally contains /home/wufnik/scripts, for cwd.pl.
i also get this behaviour if i try running
perl -e 'print `perl -S cwd.pl`'
from /tmp, where it would print out: /tmp
i would like to know what happens when you use the perl -x flag, which
"switches to that directory before running the program". can't seem to get this to change to any directory on my win32 box, running cygwin, and the -h for perl gives: 'perhaps cd to directory'. perhaps! is this a case of hubris in perl-the-app? do i have to be especially good for this cd to take place? is the sense of mystery here designed to lend yet more allure to the perl command line? anyone use this flag before? if so, what does it do to cwd?
any enlightenment, even a wee bit, appreciated greatly...
wufnik
-- in the world of the mules there are no rules --
| [reply] [d/l] [select] |
|
i would like to know what happens when you use the perl -x flag, which "switches to that directory before running the program". can't seem to get this to change to any directory on my win32 box, running cygwin, and the -h for perl gives: 'perhaps cd to directory'. perhaps! is this a case of hubris in perl-the-app? do i have to be especially good for this cd to take place? is the sense of mystery here designed to lend yet more allure to the perl command line? anyone use this flag before? if so, what does it do to cwd?
let me state the line from perl -h:
-x(directory) strip off text before #!perl line and perhaps cd to directory
"perhaps" means, if you gave -x a directory parameter,
*then* change to that directory. the -x switch is explained
much better in perldoc perlrun:
-x directory
tells Perl that the program is embedded in a larger chunk of unrelated ASCII text, such as in a mail message. Leading garbage will be discarded until the
first line that starts with #! and contains the
string "perl".....
| [reply] |
Re: current working dir is location of script?
by nimdokk (Vicar) on Jul 31, 2003 at 10:58 UTC
|
Other people have answered this question quite nicely, I just wanted to add one other point about the current working directory. Say you have a script in /home/me/bin. And it references other files in the current directory, when you run it from the commandline, this will work fine, but if you were to run it from crontab, it would not work as expected. Because when running programs from crontab, the current directory defaults to your home directory, in this case /home/me. It is usually a good idea if your program relies on the path information to other files to use absolute path names, or at least always make sure your program knows where it is before it tries to do anything with a file. Just my 2 cents :-)
"Ex libris un peut de tout" | [reply] |
Re: current working dir is location of script?
by zakzebrowski (Curate) on Jul 31, 2003 at 11:48 UTC
|
Hi,
As I recall, if you do a perldoc on File::Find, it shows that the current directory will change as a file is found (unless expressedly requested otherwise), so you can just do an open($_) on the file name and it will work as expected. (Thus, your supposition is incorrect since you can change the working directory...)
Have a good one.
----
Zak
Pluralitas non est ponenda sine neccesitate - mysql's philosphy | [reply] |
|
|