home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4984 < prev    next >
Encoding:
Text File  |  1992-07-27  |  1.5 KB  |  53 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gatech!destroyer!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!mp.cs.niu.edu!uxa.ecn.bgu.edu!psuvax1!flee
  3. From: flee@cs.psu.edu (Felix Lee)
  4. Subject: Re: or puzzlement
  5. Message-ID: <Bs2HMz.Gqp@cs.psu.edu>
  6. Sender: news@cs.psu.edu (Usenet)
  7. Nntp-Posting-Host: dictionopolis.cs.psu.edu
  8. References: <BrtMFu.D0L@unx.sas.com> <1992Jul23.191243.9731@netlabs.com>
  9. Date: Mon, 27 Jul 1992 22:42:24 GMT
  10. Lines: 41
  11.  
  12. >    opendir(D,$f) || warn("no dir $f") && return 1;
  13. >    opendir(D,$f) || do {warn "no dir $f"; return 1};
  14. >    opendir(D,$f) || (warn("no dir $f"), return 1);
  15. >    opendir(D,$f) || return warn "no dir $f";
  16.  
  17. Drowning in a surfeit of choices.
  18.  
  19. Code written like this:
  20.     if (! opendir(D, $f)) {
  21.         warn "$0: Can't open directory $f: $!\n";
  22.         return 1;
  23.     }
  24. is perfectly understandable, although not particularly compact.
  25.  
  26. Lately, I've been writing code like:
  27.     &opendir(*D, $f) || return 'fail';
  28.  
  29. where &opendir is something like
  30.     sub opendir {
  31.         local (*DIR, $name) = @_;
  32.         if (! opendir(DIR, $name)) {
  33.             warn "$0: Can't open directory $name: $!\n";
  34.             return 0;
  35.         }
  36.         return 1;
  37.     }
  38.  
  39. Packaging the error message with the operation improves the
  40. consistency and the usefulness of the diagnostics.
  41.  
  42. Perhaps some general exception handling convention, using eval/die,
  43. would be better, but I haven't felt my way around eval/die yet.
  44.  
  45.     eval { &opendir(*D, $f) } || die "$@";
  46.  
  47.     sub opendir {
  48.         local (*DIR, $name) = @_;
  49.         opendir(DIR, $name) ||
  50.             die "$0: Can't open directory $name: $!\n";
  51.     }
  52. --
  53.