Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!ogicse!hakanson From: hakanson@ogicse.ogi.edu (Marion Hakanson) Newsgroups: comp.lang.perl Subject: Re: Perl beginner Message-ID: <22198@ogicse.ogi.edu> Date: 31 May 91 21:13:39 GMT References: <1991May31.161535.2023@eci386.uucp> <1991May31.181659.28817@jpl-devvax.jpl.nasa.gov> Organization: Oregon Graduate Institute (formerly OGC), Beaverton, OR Lines: 64 In article <1991May31.181659.28817@jpl-devvax.jpl.nasa.gov> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes: >. . . >But also see pwd.pl in the Perl library. It uses the environment variable >as a guess, but runs pwd if the guess is wrong. Thenceforth it can keep >track of the current directory for you, as long as you call its chdir >routine to do all your chdirs. > >My policy heretofore has been to drag my feet on adding routines that >supply information that you only need once per process. Besides, it's >good to force C programmers to use the toolbox occasionally. :-) Second thing first: I disagree that you only need such information (the current working directory) once per process. In the presence of symbolic links, the only method I've come up with for finding the actual full path translation of a given path (relative or starting with "/") is to chdir() to the destination (or one "/" above, if it's not a directory) and to then find the "real" current directory. I have used this technique on more than one occasion, and without fail I have wished I didn't have to start up a "pwd" process to get this information. Now, to pwd.pl. It is quite unsatisfactory to me, in that it gives incorrect results in the presence of symbolic links. Under these (very common) conditions, the only way to find the current working directory is to ask the operating system. The alternative is to model the OS in your perl library. Most versions of Unix these days have a nice getwd() call -- I sure wish Perl could access it directly without running another program. ===== pwdtst ===== #!/usr/bin/perl require('pwd.pl'); &initpwd; $pwd = `pwd`; print "PWD=$ENV{'PWD'} pwd=$pwd"; &chdir($ARGV[0]) || die "Can't chdir($ARGV[0]): $!, aborted"; $pwd = `pwd`; print "PWD=$ENV{'PWD'} pwd=$pwd"; ================== === test script == Script started on Fri May 31 13:55:37 1991 ogicse 51% pwd /usr/etc ogicse 52% ls -lg local lrwxr-xr-x 1 root System 14 Apr 20 04:51 local -> /usr/local/etc ogicse 53% ~hakanson/perl/tests/pwdtst local PWD=/usr/etc pwd=/usr/etc PWD=/usr/etc/local pwd=/usr/local/etc ogicse 54% ~hakanson/perl/tests/pwdtst local/.. PWD=/usr/etc pwd=/usr/etc PWD=/usr/etc pwd=/usr/local ogicse 55% exit ogicse 56% script done on Fri May 31 13:56:51 1991 ================== -- Marion Hakanson Domain: hakanson@cse.ogi.edu UUCP : ogicse!hakanson