home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!elroy.jpl.nasa.gov!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.perl
- Subject: Re: Filename expansion ("~user")
- Message-ID: <1992Aug22.193816.15011@netlabs.com>
- Date: 22 Aug 92 19:38:16 GMT
- References: <1992Aug20.153727.167@cbnews.cb.att.com> <MERLYN.92Aug21092257@romulus.reed.edu>
- Sender: news@netlabs.com
- Distribution: usa
- Organization: NetLabs, Inc.
- Lines: 41
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <MERLYN.92Aug21092257@romulus.reed.edu> merlyn@romulus.reed.edu (Randal L. Schwartz) writes:
- : Fetching the home directory is easy enough though:
- :
- : $file = "~merlyn/.newsrc";
- : $file =~ s#^~([a-z0-9]+)(/.*)?$#(getpwnam($1))[7].$2#e;
- :
- :
- : (If you mess with $[, you deserve what you get. :-)
-
- And if you don't check for error conditions, you don't get what you deserve.
-
- $file =~ s#^(~([a-z0-9]+))(/.*)?$#((getpwnam($2))[7]||$1).$3#e;
-
- This spits out ~merlyn/.newsrc rather than /.newsrc if merlyn isn't a
- user on your box. Then you at least get a more reasonable error
- message when the open fails.
-
- Also, [a-z0-9]+ is a bit too restrictive on login names--it's not our
- program's responsibility to enforce anything other than the absence of
- a slash. What are we supposed to do if someone types "~phi-phi/.newsrc",
- and the login file contains
-
- phi-phi:LVuNVXgSiJkN2:128:17:Phi-Phi the Poodle:/home/dogs/phi-phi:/bin/csh
-
- I'd probably write it like this:
-
- $USER = $ENV{USER} || $ENV{LOGNAME} || getlogin || (getpwnam($<))[0];
- ...
- $file =~ s#^[^/]*#$user#
- if $file =~ m#^~([^/]*)# && ($user = $1 ? (getpwnam($1))[7] : $USER);
-
- I'd probably make that into a subroutine. Alternately, if I know I'm
- going to be using this only on machines with csh, and I don't care about
- the cost of starting up a csh, I'd just say
-
- $file = < $file > if $file =~ /^~/;
-
- Perl does globs using csh by preference (though it prefers /bin/sh for
- everything else).
-
- Larry
-