home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!decwrl!concert!sas!mozart.unx.sas.com!kent
- From: kent@manzi.unx.sas.com (Paul Kent)
- Newsgroups: comp.lang.perl
- Subject: Re: or puzzlement
- Message-ID: <Brut2B.80J@unx.sas.com>
- Date: 23 Jul 92 18:08:35 GMT
- References: <BrtMFu.D0L@unx.sas.com> <MERLYN.92Jul23070028@romulus.reed.edu>
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Organization: SAS Institute Inc.
- Lines: 100
- Originator: kent@manzi.unx.sas.com
- Nntp-Posting-Host: manzi.unx.sas.com
-
-
- In article <MERLYN.92Jul23070028@romulus.reed.edu>, merlyn@romulus.reed.edu (Randal L. Schwartz) writes:
- >In article <BrtMFu.D0L@unx.sas.com> kent@manzi.unx.sas.com (Paul Kent) writes:
- >
- >> i know i can do:
- >>
- >> opendir(D,$f) || do
- >> { warn "no $f";
- >> return 1;
- >> };
- >>
- >>
- >> but that seems ugly. is there a better idiom for things like
- >>
- >> dothis || warn_and_return
- >> dothis || warn_and_next
- >>
- >
- >In the stuff you're doing, you could just go:
- >
- > open(...) || warn " xxx ";
- >
- >because the return value will be the last value executed, so you'll
- >get a "1" on a successful "open", and a 0 on a "warn" (presuming that
- >warn does indeed return 0 always). Larry will have to comment on
- >whether to trust that behavior.
- >
- >If you need to return a 0 from the middle of a routine (using return),
- >I've been known to do things like:
- >
- > warn "oops!", return 0 unless open(...);
- >
- >*or*, if you can guarantee the 0 value of warn, you could even do this:
- >
- > open(...) || return warn "bad news!"
- >
- >Sure, it looks weird, but it's perlish!
- >
-
- ok, thanks -- that comma trick works nicely for the return case, but....
- i'm still searching for a useful idiom for the warn_and_next while looping
-
- perhaps "next" is not as executable as "return".
- it doesnt behave similarly... (rbj would be proud!)
-
-
-
- here are some attempts..
-
- #!/usr/local/bin/perl
-
- #
- # riddle_me_this
- #
-
-
- $d1="/etc"; # a directory
- $d2="/not"; # not a directory
-
- #
- # how to generalize that comma trick to "next" ...
- #
- # loop_or is broke cuz it doesnt "next" for a non-directory
- # loop_and is broke cuz it doesnt print the warning for a non-directory
- # loop_comma is broke cuz it doesnt print the warning for a non-directory
- # loop_paren is broke cuz it doesnt print the warning for a non-directory
- #
- print STDERR "\n\n and now for loopy tricks\n\n";
- for $d ($d1,$d2)
- {
- opendir(D, $d) || warn "loop_or: no $d" || next;
- print STDERR "loop_or: $d must exist cuz we got this far\n";
- }
-
- for $d ($d1,$d2)
- {
- opendir(D, $d) || warn "loop_and: no $d" && next;
- print STDERR "loop_and: $d must exist cuz we got this far\n";
- }
-
- for $d ($d1,$d2)
- {
- opendir(D, $d) || warn "loop_comma: no $d", next;
- print STDERR "loop_comma: $d must exist cuz we got this far\n";
- }
-
- for $d ($d1,$d2)
- {
- opendir(D, $d) || (warn "loop_comma2: no $d" && next);
- print STDERR "loop_paren: $d must exist cuz we got this far\n";
- }
-
-
-
- Cheers,
- Paul.
- --
-
- Paul Kent (SQL r&d) " nothing ventured, nothing disclaimed "
- kent@unx.sas.com SAS Institute Inc, SAS Campus Dr, Cary NC 27513-2414.
-