home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- 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
- From: flee@cs.psu.edu (Felix Lee)
- Subject: Re: or puzzlement
- Message-ID: <Bs2HMz.Gqp@cs.psu.edu>
- Sender: news@cs.psu.edu (Usenet)
- Nntp-Posting-Host: dictionopolis.cs.psu.edu
- References: <BrtMFu.D0L@unx.sas.com> <1992Jul23.191243.9731@netlabs.com>
- Date: Mon, 27 Jul 1992 22:42:24 GMT
- Lines: 41
-
- > opendir(D,$f) || warn("no dir $f") && return 1;
- > opendir(D,$f) || do {warn "no dir $f"; return 1};
- > opendir(D,$f) || (warn("no dir $f"), return 1);
- > opendir(D,$f) || return warn "no dir $f";
-
- Drowning in a surfeit of choices.
-
- Code written like this:
- if (! opendir(D, $f)) {
- warn "$0: Can't open directory $f: $!\n";
- return 1;
- }
- is perfectly understandable, although not particularly compact.
-
- Lately, I've been writing code like:
- &opendir(*D, $f) || return 'fail';
-
- where &opendir is something like
- sub opendir {
- local (*DIR, $name) = @_;
- if (! opendir(DIR, $name)) {
- warn "$0: Can't open directory $name: $!\n";
- return 0;
- }
- return 1;
- }
-
- Packaging the error message with the operation improves the
- consistency and the usefulness of the diagnostics.
-
- Perhaps some general exception handling convention, using eval/die,
- would be better, but I haven't felt my way around eval/die yet.
-
- eval { &opendir(*D, $f) } || die "$@";
-
- sub opendir {
- local (*DIR, $name) = @_;
- opendir(DIR, $name) ||
- die "$0: Can't open directory $name: $!\n";
- }
- --
-