in reply to current working dir is location of script?

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 --

Replies are listed 'Best First'.
Re: cwd, location, and the secret of -x
by wirrwarr (Monk) on Jul 31, 2003 at 13:34 UTC
    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".....