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

I am writing a program on a win32 system to navigate a Byzantine subdirectory structure.

I am including features so you can search by text or regular expressions. Then it would either jump to the directory if it’s your only match or give you a list to select from.

I have everything working except I can’t change the directory on exit of the program.

Here is what I have tried

chdir("c:\\"); #Doesn't Work system("cd c:\\"); #Doesn't Work system("start cmd -k c:\\"); #Works but opens a new command com up. Un +wanted!

I have read that perl restores the path it was executed from on exit.

Is there any way to force it to not restore or restore the path I want??

Replies are listed 'Best First'.
Re: Controlling the directory location on exit.
by John M. Dlugosz (Monsignor) on Nov 12, 2002 at 20:02 UTC
    No.

    It's not Perl restoring it. Rather, the change was made in Perl's own copy of the environment, and that vanished when Perl exited. The shell (or other program) that launched perl.exe never sees the change, and fundimentally cannot.

    In the DOS days, there was a way to do it by finding the root environment block.

    Doing this is not a capability of the OS. Instead, a specific way would be needed to communicate to the parent process and tell it to change its own local copy. I don't know if any popular command shell does this. It would be possible in the Explorer GUI shell using an add-in that runs inproc.

    What I've thought about is having the program not change directories itself (which as you know is pointless) but communicate the new directory name back to the caller somehow. Then, run that from a BAT file (which can change directories).

    Example,

    REM this is fancyCD.BAT echo "chdir " > temp.bat fancyCD.pl >> temp.bat temp
    Where the perl script writes the choice to standard output.

    —John

Re: Controlling the directory location on exit.
by the pusher robot (Monk) on Nov 12, 2002 at 19:23 UTC
    Is there any way to force it to not restore or restore the path I want??

    I don't think so. For security reasons, programs are not able to mess with their parent's environment. On win32, using cd within a batch file may (or may not) work; if so, you can possibly write a batch file that reads the output of your script and calls cd if appropriate. Other than that, I don't think there's a way to do it.
(z) Re: Controlling the directory location on exit.
by zigdon (Deacon) on Nov 12, 2002 at 19:21 UTC
    I don't think you can do that. Basicly, you want a process to change it's paren't directory. I don't know of a way to do that.

    -- Dan