home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4908 < prev    next >
Encoding:
Internet Message Format  |  1992-07-23  |  1.8 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!cs.utexas.edu!uwm.edu!ogicse!reed!romulus!merlyn
  2. From: merlyn@romulus.reed.edu (Randal L. Schwartz)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: or puzzlement
  5. Message-ID: <MERLYN.92Jul23070028@romulus.reed.edu>
  6. Date: 23 Jul 92 14:00:31 GMT
  7. Article-I.D.: romulus.MERLYN.92Jul23070028
  8. References: <BrtMFu.D0L@unx.sas.com>
  9. Sender: news@reed.edu (USENET News System)
  10. Organization: Reed College
  11. Lines: 43
  12. In-Reply-To: kent@manzi.unx.sas.com's message of 23 Jul 92 02:47:54 GMT
  13.  
  14. In article <BrtMFu.D0L@unx.sas.com> kent@manzi.unx.sas.com (Paul Kent) writes:
  15.  
  16.    i know i can do:
  17.  
  18.    opendir(D,$f) || do
  19.    { warn "no $f";
  20.      return 1;
  21.    };
  22.  
  23.  
  24.    but that seems ugly. is there a better idiom for things like
  25.  
  26.     dothis || warn_and_return
  27.     dothis || warn_and_next
  28.  
  29. In the stuff you're doing, you could just go:
  30.  
  31.     open(...) || warn " xxx ";
  32.  
  33. because the return value will be the last value executed, so you'll
  34. get a "1" on a successful "open", and a 0 on a "warn" (presuming that
  35. warn does indeed return 0 always).  Larry will have to comment on
  36. whether to trust that behavior.
  37.  
  38. If you need to return a 0 from the middle of a routine (using return),
  39. I've been known to do things like:
  40.  
  41.     warn "oops!", return 0 unless open(...);
  42.  
  43. *or*, if you can guarantee the 0 value of warn, you could even do this:
  44.  
  45.     open(...) || return warn "bad news!"
  46.  
  47. Sure, it looks weird, but it's perlish!
  48.  
  49. (It's funny, but while I was constructing my JAPH, I discovered that
  50. *my* warn is returning 1.  Are you *sure* you are getting 0?)
  51.  
  52. warn "Just another Perl hacker,\n" || die;
  53. --
  54. Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
  55. merlyn@reed.edu (guest account) merlyn@ora.com (better for permanent record)
  56. cute quote: "Welcome to Portland, Oregon -- home of the California Raisins!"
  57.