home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5665 < prev    next >
Encoding:
Text File  |  1992-09-01  |  1.5 KB  |  65 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gatech!concert!sas!mozart.unx.sas.com!kent
  3. From: kent@manzi.unx.sas.com (Paul Kent)
  4. Subject: nuther perl-idiom question..
  5. Originator: kent@manzi.unx.sas.com
  6. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  7. Message-ID: <Btx90n.BMG@unx.sas.com>
  8. Date: Tue, 1 Sep 1992 22:55:35 GMT
  9. Nntp-Posting-Host: manzi.unx.sas.com
  10. Organization: SAS Institute Inc.
  11. Lines: 52
  12.  
  13.  
  14.  
  15. in kornshell, one can say:
  16.  
  17.   x=${foo:-default}
  18.  
  19. which assigns x the value of foo if foo is defined, and "default" otherwise...
  20.  
  21.  
  22.  
  23. in perl, one could:
  24.  
  25.   $x = defined($foo) ? $foo : "default";
  26.  
  27.  
  28. but that gets ugly, specially when foo is an assoc array.
  29.  
  30.   $x = defined($foo{'this'}) ? $foo{'this'} : "default";
  31.  
  32.  
  33.  
  34.  
  35.  
  36. is there a better idiom. (other than loading the assoc array with
  37. the defaults before one starts, which doesnt help if the "default"
  38. varies from one spot to another in the code)
  39.  
  40. is there an idiom that references $foo once only?
  41.  
  42. best i could find is.. (and it doesnt handle the boundary of a "false" $foo{'this'}
  43.  
  44.   $x = $foo{'this'};
  45.   $x = "default" unless $x;
  46.  
  47. the double assignment of x feels wrong, even if it is mostly elided.
  48.  
  49.  
  50.  
  51. this gets the same kornshell semantics, but the initial assigment of
  52. $x seems rather buried.. ones eyes would tend to think that x didnt
  53. get assigned by the statement unless...
  54.  
  55.   $x = "default" unless defined($x=$foo{'this'});
  56.  
  57.  
  58. what say?
  59. paul
  60.  
  61. -- 
  62.  
  63. Paul Kent (SQL r&d)                   " nothing ventured, nothing disclaimed "
  64. kent@unx.sas.com         SAS Institute Inc, SAS Campus Dr, Cary NC 27513-2414.
  65.