home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5433 < prev    next >
Encoding:
Internet Message Format  |  1992-08-22  |  1.9 KB

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