Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl beginner

24 views
Skip to first unread message

Robert Steven Glickstein

unread,
May 28, 1991, 9:26:35 PM5/28/91
to
I'm a Perl beginner, but I'm amazed by how quickly I've picked up the
language and how quickly I've been able to write really complex
utilities. To an experienced C programmer like myself, Perl came very
naturally and it is now my prototype/utility language of choice. OK,
enough hype. :-)

It seems that Perl ought to have a built-in getwd command, but it
doesn't. Is it the case that the only way to find out the current
working directory is to do

$cwd = `/bin/pwd`;
chop $cwd;

?

Thanks in advance.

______________ _____________________________
Bob Glickstein | Internet: bo...@andrew.cmu.edu
Information Technology Center | Bitnet: bobg%and...@cmuccvma.bitnet
Carnegie Mellon University | UUCP: ...!harvard!andrew.cmu.edu!bobg
Pittsburgh, PA 15213-3890 |
(412) 268-6743 | Sinners can repent, but stupid is forever

Kartik Subbarao

unread,
May 29, 1991, 8:34:48 AM5/29/91
to
In article <EcEkB=i00Vsn...@andrew.cmu.edu> bo...@andrew.cmu.edu (Robert Steven Glickstein) writes:
>I'm a Perl beginner, but I'm amazed by how quickly I've picked up the
>language and how quickly I've been able to write really complex
>utilities. To an experienced C programmer like myself, Perl came very
>naturally and it is now my prototype/utility language of choice. OK,
>enough hype. :-)
>
>It seems that Perl ought to have a built-in getwd command, but it
>doesn't.

By "built-in", do you mean that it should getcwd() every time it starts up
and save that as a $something variable? There's really no need to do so, if
you don't care about your current working directory. SOMEONE has to do the
getcwd() call -- you don't get it for free.

Is it the case that the only way to find out the current
>working directory is to do
>
> $cwd = `/bin/pwd`;
> chop $cwd;

In the case that your shell sets a CWD or PWD environment variable, then you
can access it in perl by:

$cwd = $ENV{'CWD'} (or PWD, or whatever).


-Kartik
--
internet% whoami

subb...@phoenix.Princeton.EDU -| Internet
kar...@silvertone.Princeton.EDU (NeXT mail)
SUBB...@PUCC.BITNET - Bitnet

John Macdonald

unread,
May 31, 1991, 12:15:35 PM5/31/91
to
In article <azWk8O...@idunno.Princeton.EDU> subb...@phoenix.Princeton.EDU (Kartik Subbarao) writes:
|In article <EcEkB=i00Vsn...@andrew.cmu.edu> bo...@andrew.cmu.edu (Robert Steven Glickstein) writes:
|
|Is it the case that the only way to find out the current
|>working directory is to do
|>
|> $cwd = `/bin/pwd`;
|> chop $cwd;
|
|In the case that your shell sets a CWD or PWD environment variable, then you
|can access it in perl by:
|
|$cwd = $ENV{'CWD'} (or PWD, or whatever).

Be careful here - this only works if you will never have the Perl
script run from a program or script written for an interpreter that
does not keep $CWD correct. Tools should not be written to depend
on being run in a very specialized context that can't be validated,
unless you are absolutely sure that the will never be run from a
different context (or evolve into something that will be run from
a different context - there are many "temporary" programs around
whose ages are measured in decades).
--
Usenet is [like] the group of people who visit the | John Macdonald
park on a Sunday afternoon. [...] luckily, most of | jmm@eci386
the people are staying on the paths and not pissing |
on the flowers - Gene Spafford

Larry Wall

unread,
May 31, 1991, 2:16:59 PM5/31/91
to
In article <1991May31....@eci386.uucp> j...@eci386.UUCP (John Macdonald) writes:

: In article <azWk8O...@idunno.Princeton.EDU> subb...@phoenix.Princeton.EDU (Kartik Subbarao) writes:
: |In article <EcEkB=i00Vsn...@andrew.cmu.edu> bo...@andrew.cmu.edu (Robert Steven Glickstein) writes:
: |
: |Is it the case that the only way to find out the current
: |>working directory is to do
: |>
: |> $cwd = `/bin/pwd`;
: |> chop $cwd;
: |
: |In the case that your shell sets a CWD or PWD environment variable, then you
: |can access it in perl by:
: |
: |$cwd = $ENV{'CWD'} (or PWD, or whatever).
:
: Be careful here - this only works if you will never have the Perl
: script run from a program or script written for an interpreter that
: does not keep $CWD correct. Tools should not be written to depend
: on being run in a very specialized context that can't be validated,
: unless you are absolutely sure that the will never be run from a
: different context (or evolve into something that will be run from
: a different context - there are many "temporary" programs around
: whose ages are measured in decades).

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. :-)

Larry

Marion Hakanson

unread,
May 31, 1991, 5:13:39 PM5/31/91
to
In article <1991May31.1...@jpl-devvax.jpl.nasa.gov> lw...@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: haka...@cse.ogi.edu
UUCP : ogicse!hakanson

0 new messages