home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / perl / 5424 < prev    next >
Encoding:
Text File  |  1992-08-21  |  2.3 KB  |  68 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!xoanon.dev.cdx.mot.com!mcook
  3. From: mcook@xoanon.dev.cdx.mot.com (Michael Cook)
  4. Subject: Re: Filename expansion ("~user")
  5. Message-ID: <mcook.714424590@xoanon.dev.cdx.mot.com>
  6. Keywords: ~user filename expansion
  7. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  8. Nntp-Posting-Host: xoanon.dev.cdx.mot.com
  9. Organization: Motorola Codex, Canton, Massachusetts
  10. References: <1992Aug20.153727.167@cbnews.cb.att.com> <1992Aug21.183318.11200@merlin.dev.cdx.mot.com>
  11. Distribution: usa
  12. Date: Fri, 21 Aug 1992 19:16:30 GMT
  13. Lines: 53
  14.  
  15. lezz@merlin.dev.cdx.mot.com (Lezz Giles) writes:
  16.  
  17. >In article <1992Aug20.153727.167@cbnews.cb.att.com>, daleske@cbnews.cb.att.com (John D. Daleske) writes:
  18. >|>I'm relatively new to Perl and love it.  I've read the recent notes in this
  19. >|>notes group and have not found how do one capability that I am trying to
  20. >|>use in a Perl script which is the ~user filename expansion.  Following is a test
  21. >|>script:
  22. >|>
  23. >|>$tst1="~daleske";
  24. >|>die "${tst1} not a directory" if !(-d ${tst1});
  25. >|>opendir(DH, ${tst1});
  26. >|>@allfiles = readdir(DH);
  27. >|>closedir(DH);
  28. >|>print "@allfiles\n";
  29. >|>
  30. >|>If I set $tst1 to the full pathname it works fine.  I'd like to use the common
  31. >|>facility of ~user to avoid needing to know the full path name.  (User home
  32. >|>directories do get moved around.)
  33.  
  34. >You'll certainly get the standard response about "~ is only a shell construct
  35. >and so you'll need to use a shell to expand it", which is valid enough and
  36. >leads to various solutions.  However, some time ago I was working in an
  37. >environment where we wanted to write /bin/sh scripts which could access
  38. >people's home directories - so we had a semi-standard tool called 'logdir'
  39. >which takes a username and returns the home directory, e.g.
  40.  
  41. >$ logdir lezz
  42. >/usr/u/a1/lezz
  43.  
  44. >Once you've written this tool, you can add lines like these to your perl prog:
  45.  
  46. >($homedir = `logdir lezz`) || die "Can't find home dir for lezz\n";
  47.  
  48. >$homebin = `logdir lezz` . "/bin";
  49.  
  50. >The source for logdir is:
  51.  
  52. [C source code deleted.]
  53.  
  54. How about this:
  55.  
  56.     #! /bin/ksh -p
  57.     for i ;do eval echo \~$i ;done
  58.  
  59. Or, the Perl code (er, this is comp.lang.perl, isn't it :-) would be:
  60.  
  61.     sub logdir
  62.     {
  63.         getpwnam($_[0])[7];
  64.     }
  65.     ($homedir = &logdir(lezz)) || die "Can't find home dir for lezz\n";
  66.  
  67. Michael.
  68.