jatill has asked for the wisdom of the Perl Monks concerning the following question:

I want to run a perl script that takes an argument. When the script is finished, I want to be in a different directory than I started. Is that possible? I'm willing to use any additional unix toys such as aliases or shell scripts if necessary.

Replies are listed 'Best First'.
Re: How to make perl affect my shell
by samizdat (Vicar) on Sep 14, 2005 at 19:39 UTC
    If you have your program exec a new shell, you will end up there.
    #!/usr/bin/perl use strict; # idiot use warnings; # pruf chdir '/home/dwilde'; exec '/usr/local/bin/bash';
    As has been pointed out, you will be in a #new# shell.
Re: How to make perl affect my shell
by jatill (Beadle) on Sep 14, 2005 at 19:37 UTC
    Looks like I found an answer to my own question. I just needed to set up my alias with a \!^ to pass along the param to perl. ex: alias warp 'cd `warp.pl \!^`' and then have the perl script print out the destination directory.
Re: How to make perl affect my shell
by Hue-Bond (Priest) on Sep 14, 2005 at 19:33 UTC

    Yes, but not in the same shell process that you "came from". Your Perl script would need to spawn another shell after chdir'ing to the new directory. You could also have a modified environment if you wanted to.

    --
    David Serrano

Re: How to make perl affect my shell
by zigdon (Deacon) on Sep 15, 2005 at 11:30 UTC
    The way I've done it is I have an alias in the shell that sources a file that's created by the perl script:
    apocalypse$ which c c is an alias for . ~/bin/mychdir $ cat ~/bin/mychdir # ~/bin/finddir $1 $2 $3 $4 $5 $6 $7 $8 $9 cd "`cat $HOME/.targetdir`"
    The benefit is that it stays within the same shell, which means my history and background jobs are all still there.

    -- zigdon