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

I am comparing the output of a very simple script run on 2 different windows 2003 machines in directory c:\temp. Both machines have similar dual implementations of perl (5.6.1 and 5.8.8). The 5.6.1 is part of a separate independent application install. There are many administration related scripts written for this separate independent application that utilize this 5.6.1 implementation. The 5.8.8 is the release all our corporate custom scripts use.
# use Cwd; print "\ncd=" . `cd`; print "\ngetcwd=" . getcwd(); #
On machine1, I get what you would expect ... both print outputs are "c:\temp". However, on machine2 when I use version 5.8.8 the script returns what one would expect, but when it uses the 5.6.1 install, it returns "cd=c:\" and "c:\temp". Why in the world could it be different? How I found this was when I was using one of the before mentioned administration scripts, I kept receiving a "file not found" error. After quite a while of debugging, I found the calls and soon learned that if I moved the file to c:\, the files were then found and the script would run fine. I then narrowed down the symptoms to the above statements so I wouldn't have to wade through a lot of residual noise. One of the things I tried to try to change the outcome was to just add a chdir command. However, it did not work since it still returned "c:\" for the first print output and "c:\temp\ldif" for the second.
# use Cwd; chdir 'c:\temp\ldif'; print "\ncd=" . `cd`; print "\ngetcwd=" . getcwd(); #
I have tried it on other machines as well with similar "dual implementations". Most returned the expected consistent results. However, one other machine returned the inconsistent result. The only thing I could see which was similar with machine 2 was both had SFU (windows services for unix) installed. So, I tried to uninstall SFU on machine 2 and restart. Unfortunately it did not help. I have reviewed all environmental vars on the two machines and now that SFU is no longer installed, there are no significant differences. Has anyone experienced anything similar? Could anyone suggest where I might look next? Thanks, BLC SoCal

Replies are listed 'Best First'.
Re: cd pwd dir chdir version default directory
by graff (Chancellor) on Dec 04, 2007 at 00:34 UTC
    Is it possible that the odd-ball machine2 happens to have some unexpected difference in the shell configuration that is used to run the perl script? If so, this would extend to the environment used to run a "cd" command via backticks (in a subshell). For example, it may be that a subshell "cd" command runs a different .exe file compared to what the other machines are doing. (Just a guess)

    In particular, a unix-like "cd" command would actually change the current working directory to the "home" directory for the shell, rather than reporting the current shell directory path.

      Exactly. I think I could make a pretty good living off of your guesses. I am too embarrassed to mention how much time I spent on this. Yes, in "hkey_current_user\software\microsoft\command processor" you can add a dword called "autorun" and basically issue any command you want. On machine2, it had "cd \". After deleting that entry in resedit, the script works find. Thank you very much. BLC

        Yes, this is a chronic nuisance when writing portable Perl scripts under Windows. Because our scripts run on many different customer machines, we don't have the option of removing the autorun value from the Registry. There are two ways around this:

        • Use CMD /D to disable autorun; for example, print "\ncd=" . `cmd /d/c cd`; in your example program.
        • Contrive to execute the command without invoking a shell; for example, system { $exe } @args, where $exe is the executable name and @args are its arguments (see perldoc -f exec for more details). Though more work, this approach leads to programs that are more robust and a bit more efficient, in that no shell process is created.