home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!munnari.oz.au!yarra-glen.aaii.oz.au!pem
- From: pem@yarra-glen.aaii.oz.au (Paul E. Maisano)
- Subject: Re: LVALUE =~ EXPR ?
- Message-ID: <1992Sep4.050628.22356@yarra-glen.aaii.oz.au>
- Organization: Australian Artificial Intelligence Institute
- References: <1992Aug29.053235.3664@yarra-glen.aaii.oz.au>
- Date: Fri, 4 Sep 1992 05:06:28 GMT
- Lines: 76
-
- Paul E. Maisano (that's me) writes:
-
- >I was rather expecting (hoping for?) this to work:
- >
- >($x = 'ab') =~ (s/a/A/, s/b/B/);
- >print "$x\n";
-
- Thanks to those who replied to my message (by mail).
- Especially Raymond Chen, who gave me a detailed explanation of
- how the expression above was actually being interpreted (which
- I'll summarize below).
-
- Firstly, my motivation for wanting the above to work was that I wanted to
- avoid repetitions such as:
- $some_meaningfully_named_variable =~ tr/abc/xyz/;
- $some_meaningfully_named_variable =~ s/foo/bar/;
- $some_meaningfully_named_variable =~ s/BAR/puppy/;
-
- I wanted to be able to say something like:
- $some_meaningfully_named_variable =~ (tr/abc/xyz/, s/foo/bar/, s/BAR/puppy/);
- Much nicer, I think.
-
- As Raymond pointed out to me, this evaluates just as described in the
- manual (once you understand what the manual is trying to say).
-
- > (If the right argument is an
- > expression other than a search pattern, substitu-
- > tion, or translation, it is interpreted as a search
- > pattern at run time.
-
- What this means is that (and it is obvious to me now), the RHS is evaluated
- just as it normally is and whatever it returns is treated as a search-pattern;
- as if it was enclosed in `/ ... /'. So the above is evaluated like this:
- $tmp = (tr/abc/xyz/, s/foo/bar/, s/BAR/puppy/);
- $some_meaningfully_named_variable =~ /$tmp/;
-
- Of course, $tmp contains the return value of the s/BAR/puppy/.
- This is not the effect I wanted. I suppose I was hoping for a bit
- of perl magic where it does what you expect it to do rather than
- what the rules say it should do :-)
-
- Ok, I know that this is not an earth-shattering problem but I hate
- repeating the variable name all over the place. So what are my options?
-
- $_ = $some_meaningfully_named_variable;
- (tr/abc/xyz/, s/foo/bar/, s/BAR/puppy/);
-
- But I might have been using $_ for something else.
-
- Probably this is best:
-
- { local($_) = ($some_meaningfully_named_variable);
- tr/abc/xyz/;
- s/foo/bar/;
- s/BAR/puppy/;
- }
-
- The syntax I would really like (and it looks like perl to me :-) is:
-
- $some_meaningfully_named_variable =~ {
- tr/abc/xyz/;
- s/foo/bar/;
- s/BAR/puppy/;
- };
-
- I think this fits in with the way braces are used for grouping things
- in perl and it seems just to be a generalization of the =~ operator.
-
- Well, I've bored you all enough by now.
-
-
- -paul, (can't wait for 5.0)
-
- ---
- Paul Maisano pem@aaii.oz.au
- Australian Artificial Intelligence Institute
-