Cwd::chdir not useless after all

runrig on 2007-09-20T22:28:34

Prior to today, I always considered Cwd::chdir() rather pointless. It keeps $ENV{PWD} up to date. But if you wanted $ENV{PWD}, why not just call Cwd::cwd() (unless maybe you wanted PWD when the program started, but then you wouldn't need Cwd at all)? Well, we had this awful code:

open( FH, "pwd|" ) or die "....: $!";
my $dir = ;
chomp $dir;
It was awful, but it worked, at least until we migrated this code to a system where '/foo/bar' was a symbolic link to '/foo/blah/bar' (and there was logic that depended on stuff being in '/foo/bar'). pwd in /foo/bar from the command line returned /foo/bar. But pwd from within perl returned /foo/blah/bar. Cwd::cwd() also returned /foo/blah/bar. But $ENV{PWD} returned /foo/bar. And we chdir'd a lot. So Cwd::chdir() saved us from changing some hardcoded /foo/bar's, and gave me an excuse to replace the awful code above with:
my $dir = $ENV{PWD};
And actually, the code is really just trying to recursively search directories, so I really ought to use File::Find...but that's more refactoring than I'm willing to do at this point :-)