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

  1. Path: sparky!uunet!olivea!decwrl!concert!sas!mozart.unx.sas.com!kent
  2. From: kent@manzi.unx.sas.com (Paul Kent)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: or puzzlement
  5. Message-ID: <Brut2B.80J@unx.sas.com>
  6. Date: 23 Jul 92 18:08:35 GMT
  7. References: <BrtMFu.D0L@unx.sas.com> <MERLYN.92Jul23070028@romulus.reed.edu>
  8. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  9. Organization: SAS Institute Inc.
  10. Lines: 100
  11. Originator: kent@manzi.unx.sas.com
  12. Nntp-Posting-Host: manzi.unx.sas.com
  13.  
  14.  
  15. In article <MERLYN.92Jul23070028@romulus.reed.edu>, merlyn@romulus.reed.edu (Randal L. Schwartz) writes:
  16. >In article <BrtMFu.D0L@unx.sas.com> kent@manzi.unx.sas.com (Paul Kent) writes:
  17. >
  18. >>   i know i can do:
  19. >>
  20. >>   opendir(D,$f) || do
  21. >>   { warn "no $f";
  22. >>     return 1;
  23. >>   };
  24. >>
  25. >>
  26. >>   but that seems ugly. is there a better idiom for things like
  27. >>
  28. >>    dothis || warn_and_return
  29. >>    dothis || warn_and_next
  30. >>
  31. >
  32. >In the stuff you're doing, you could just go:
  33. >
  34. >    open(...) || warn " xxx ";
  35. >
  36. >because the return value will be the last value executed, so you'll
  37. >get a "1" on a successful "open", and a 0 on a "warn" (presuming that
  38. >warn does indeed return 0 always).  Larry will have to comment on
  39. >whether to trust that behavior.
  40. >
  41. >If you need to return a 0 from the middle of a routine (using return),
  42. >I've been known to do things like:
  43. >
  44. >    warn "oops!", return 0 unless open(...);
  45. >
  46. >*or*, if you can guarantee the 0 value of warn, you could even do this:
  47. >
  48. >    open(...) || return warn "bad news!"
  49. >
  50. >Sure, it looks weird, but it's perlish!
  51. >
  52.  
  53. ok, thanks -- that comma trick works nicely for the return case, but....
  54. i'm still searching for a useful idiom for the warn_and_next while looping
  55.  
  56. perhaps "next" is not as executable as "return". 
  57. it doesnt behave similarly... (rbj would be proud!)
  58.  
  59.  
  60.  
  61. here are some attempts..
  62.  
  63. #!/usr/local/bin/perl
  64.  
  65. #
  66. # riddle_me_this
  67. #
  68.  
  69.  
  70. $d1="/etc";                     # a directory
  71. $d2="/not";                     # not a directory
  72.  
  73. #
  74. # how to generalize that comma trick to "next" ...
  75. #
  76. # loop_or    is broke cuz it doesnt "next" for a non-directory
  77. # loop_and   is broke cuz it doesnt print the warning for a non-directory
  78. # loop_comma is broke cuz it doesnt print the warning for a non-directory
  79. # loop_paren is broke cuz it doesnt print the warning for a non-directory
  80. #
  81. print STDERR "\n\n and now for loopy tricks\n\n";
  82. for $d ($d1,$d2)
  83. {
  84.     opendir(D, $d) || warn "loop_or: no $d" || next;
  85.     print STDERR "loop_or: $d must exist cuz we got this far\n"; 
  86. }
  87.  
  88. for $d ($d1,$d2)
  89. {
  90.     opendir(D, $d) || warn "loop_and: no $d" && next;
  91.     print STDERR "loop_and: $d must exist cuz we got this far\n"; 
  92. }
  93.  
  94. for $d ($d1,$d2)
  95. {
  96.     opendir(D, $d) || warn "loop_comma: no $d", next;
  97.     print STDERR "loop_comma: $d must exist cuz we got this far\n"; 
  98. }
  99.  
  100. for $d ($d1,$d2)
  101. {
  102.     opendir(D, $d) || (warn "loop_comma2: no $d" && next);
  103.     print STDERR "loop_paren: $d must exist cuz we got this far\n"; 
  104. }
  105.  
  106.  
  107.  
  108. Cheers,
  109. Paul.
  110. -- 
  111.  
  112. Paul Kent (SQL r&d)                   " nothing ventured, nothing disclaimed "
  113. kent@unx.sas.com         SAS Institute Inc, SAS Campus Dr, Cary NC 27513-2414.
  114.