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

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!munnari.oz.au!yarra-glen.aaii.oz.au!pem
  3. From: pem@yarra-glen.aaii.oz.au (Paul E. Maisano)
  4. Subject: Re: LVALUE =~ EXPR ?
  5. Message-ID: <1992Sep4.050628.22356@yarra-glen.aaii.oz.au>
  6. Organization: Australian Artificial Intelligence Institute
  7. References: <1992Aug29.053235.3664@yarra-glen.aaii.oz.au>
  8. Date: Fri, 4 Sep 1992 05:06:28 GMT
  9. Lines: 76
  10.  
  11. Paul E. Maisano (that's me) writes:
  12.  
  13. >I was rather expecting (hoping for?) this to work:
  14. >
  15. >($x = 'ab') =~ (s/a/A/, s/b/B/);
  16. >print "$x\n";
  17.  
  18. Thanks to those who replied to my message (by mail).
  19. Especially Raymond Chen, who gave me a detailed explanation of
  20. how the expression above was actually being interpreted (which
  21. I'll summarize below).
  22.  
  23. Firstly, my motivation for wanting the above to work was that I wanted to
  24. avoid repetitions such as:
  25.     $some_meaningfully_named_variable =~ tr/abc/xyz/;
  26.     $some_meaningfully_named_variable =~ s/foo/bar/;
  27.     $some_meaningfully_named_variable =~ s/BAR/puppy/;
  28.  
  29. I wanted to be able to say something like:
  30. $some_meaningfully_named_variable =~ (tr/abc/xyz/, s/foo/bar/, s/BAR/puppy/);
  31. Much nicer, I think.
  32.  
  33. As Raymond pointed out to me, this evaluates just as described in the
  34. manual (once you understand what the manual is trying to say).
  35.  
  36. >                                     (If the right argument is an
  37. >             expression other than a  search  pattern,  substitu-
  38. >             tion,  or translation, it is interpreted as a search
  39. >             pattern at run time.
  40.  
  41. What this means is that (and it is obvious to me now), the RHS is evaluated
  42. just as it normally is and whatever it returns is treated as a search-pattern;
  43. as if it was enclosed in `/ ... /'. So the above is evaluated like this:
  44.     $tmp = (tr/abc/xyz/, s/foo/bar/, s/BAR/puppy/);
  45.     $some_meaningfully_named_variable =~ /$tmp/;
  46.  
  47. Of course, $tmp contains the return value of the s/BAR/puppy/.
  48. This is not the effect I wanted. I suppose I was hoping for a bit
  49. of perl magic where it does what you expect it to do rather than
  50. what the rules say it should do :-)
  51.  
  52. Ok, I know that this is not an earth-shattering problem but I hate
  53. repeating the variable name all over the place. So what are my options?
  54.  
  55.     $_ = $some_meaningfully_named_variable;
  56.     (tr/abc/xyz/, s/foo/bar/, s/BAR/puppy/);
  57.  
  58. But I might have been using $_ for something else.
  59.  
  60. Probably this is best:
  61.  
  62.     { local($_) = ($some_meaningfully_named_variable);
  63.       tr/abc/xyz/;
  64.       s/foo/bar/;
  65.       s/BAR/puppy/;
  66.     }
  67.  
  68. The syntax I would really like (and it looks like perl to me :-) is:
  69.  
  70.     $some_meaningfully_named_variable =~ {
  71.         tr/abc/xyz/;
  72.         s/foo/bar/;
  73.         s/BAR/puppy/;
  74.     };
  75.  
  76. I think this fits in with the way braces are used for grouping things
  77. in perl and it seems just to be a generalization of the =~ operator.
  78.  
  79. Well, I've bored you all enough by now.
  80.  
  81.  
  82. -paul, (can't wait for 5.0)
  83.  
  84. ---
  85. Paul Maisano                  pem@aaii.oz.au
  86. Australian Artificial Intelligence Institute
  87.