home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!gatech!concert!sas!mozart.unx.sas.com!kent
- From: kent@manzi.unx.sas.com (Paul Kent)
- Subject: nuther perl-idiom question..
- Originator: kent@manzi.unx.sas.com
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Message-ID: <Btx90n.BMG@unx.sas.com>
- Date: Tue, 1 Sep 1992 22:55:35 GMT
- Nntp-Posting-Host: manzi.unx.sas.com
- Organization: SAS Institute Inc.
- Lines: 52
-
-
-
- in kornshell, one can say:
-
- x=${foo:-default}
-
- which assigns x the value of foo if foo is defined, and "default" otherwise...
-
-
-
- in perl, one could:
-
- $x = defined($foo) ? $foo : "default";
-
-
- but that gets ugly, specially when foo is an assoc array.
-
- $x = defined($foo{'this'}) ? $foo{'this'} : "default";
-
-
-
-
-
- is there a better idiom. (other than loading the assoc array with
- the defaults before one starts, which doesnt help if the "default"
- varies from one spot to another in the code)
-
- is there an idiom that references $foo once only?
-
- best i could find is.. (and it doesnt handle the boundary of a "false" $foo{'this'}
-
- $x = $foo{'this'};
- $x = "default" unless $x;
-
- the double assignment of x feels wrong, even if it is mostly elided.
-
-
-
- this gets the same kornshell semantics, but the initial assigment of
- $x seems rather buried.. ones eyes would tend to think that x didnt
- get assigned by the statement unless...
-
- $x = "default" unless defined($x=$foo{'this'});
-
-
- what say?
- paul
-
- --
-
- Paul Kent (SQL r&d) " nothing ventured, nothing disclaimed "
- kent@unx.sas.com SAS Institute Inc, SAS Campus Dr, Cary NC 27513-2414.
-