in reply to Re: shell cd within perl script
in thread shell cd within perl script

A FAQ, no less. Thanks. I'll wrap the perl script in a shell script or somesuch.

qq

Replies are listed 'Best First'.
Re^3: shell cd within perl script
by Roy Johnson (Monsignor) on Apr 19, 2004 at 15:19 UTC
    Your question can be read two ways. Do you want the user's current directory to be changed after the program exits, or only during the execution of the program? Most of the answers address the latter interpretation, which is an odd requirement. Note that (as another monk has suggested) you can use chdir to change directories within the script, and it will apply to everything spawned by the script from then on, as well as to any operations the script itself does.

    The PerlMonk tr/// Advocate
Re: Re: Re: shell cd within perl script
by sgifford (Prior) on Apr 19, 2004 at 15:50 UTC

    A shell script can't do it either, for much the same reason. In most shells you can use a shell function to affect the current shell's environment. If your Perl script needs to determine what directory to go to, you can just have it print that out, then use cd `your_script` in the shell function to actually change to that directory.

    For example, here's a Bash shell function using a perl script:

    function test() { cd `perl -e 'print "/tmp\n";'`; }

      This is essentially the solution I've adopted. My perl script just creates shortcuts through a complicated directory structure. I'm running it like: cd `script.pl arg`

      qq