home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5598 < prev    next >
Encoding:
Text File  |  1992-08-29  |  2.3 KB  |  65 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: LVALUE =~ EXPR ?
  5. Message-ID: <1992Aug29.053235.3664@yarra-glen.aaii.oz.au>
  6. Organization: Australian Artificial Intelligence Institute
  7. Date: Sat, 29 Aug 1992 05:32:35 GMT
  8. Lines: 55
  9.  
  10. [Apologies if anyone receives this message twice...]
  11.  
  12. I was rather expecting (hoping for?) this to work:
  13.  
  14. ($x = 'ab') =~ (s/a/A/, s/b/B/);
  15. print "$x\n";
  16.  
  17. I was expecting the output to be "AB".
  18. I thought the =~ would modify the context for the whole RHS.
  19. Well, to be honest, I was expecting either that or a syntax error.
  20.  
  21. There was no syntax error but it did not do what I expected either.
  22. I read the manual to see what it said but I found it a little hard to
  23. work out exactly what it meant.
  24.  
  25. [Here is what it says to save you the trouble of looking it up.
  26.  
  27.      =~      Certain operations search or modify the string  "$_"
  28.              by default.  This operator makes that kind of opera-
  29.              tion work on some other string.  The right  argument
  30.              is  a  search pattern, substitution, or translation.
  31.              The  left  argument  is  what  is  supposed  to   be
  32.              searched,  substituted, or translated instead of the
  33.              default "$_".  The return value indicates  the  suc-
  34.              cess of the operation.  (If the right argument is an
  35.              expression other than a  search  pattern,  substitu-
  36.              tion,  or translation, it is interpreted as a search
  37.              pattern at run time.  This is less efficient than an
  38.              explicit  search, since the pattern must be compiled
  39.              every time the expression is  evaluated.)  The  pre-
  40.              cedence  of  this operator is lower than unary minus
  41.              and autoincrement/decrement, but higher than  every-
  42.              thing else.]
  43.  
  44. Does anyone think the above should work ?
  45.  
  46. Or are we stuck with:
  47.  
  48. $x = 'ab';
  49. $x =~ s/a/A/;
  50. $x =~ s/b/B/;
  51.  
  52. Or more likely,
  53.  
  54. $_ = 'ab', s/a/A/, s/b/B/;
  55.  
  56. I really like the way perl lets you change the context for substitions
  57. (and matches and translates) using =~ (I find it quite intuitive) but
  58. I was hoping it was a little more general.
  59.  
  60. -paul
  61.  
  62. ---
  63. Paul Maisano                  pem@aaii.oz.au
  64. Australian Artificial Intelligence Institute
  65.