Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!convex!news From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: s///e bizarreness Message-ID: <1991Jan03.123139.13432@convex.com> Date: 3 Jan 91 12:31:39 GMT References: <1991Jan3.043016.27394@uvaarpa.Virginia.EDU> Sender: news@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 67 Nntp-Posting-Host: pixel.convex.com From the keyboard of marc@mit.edu: :I noticed this strange behavior yesterday: : :% perl -e '$_="|2+2|";s/\|([^\|]*)\|/$1/;print "$_\n";' :2+2 :% perl -e '$_="|2+2|";s/\|([^\|]*)\|/$1/e;print "$_\n";' :2+2 :% perl -e '$_="|2+2|";s/\|([^\|]*)\|/$1/ee;print "$_\n";' :4 :% perl -v : : :The first one makes sense. The last two are what's confusing me. Is :this a bug, or an undocumented feature (or both? :-) I tested this on :a vax running bsd 4.3, perl compiled with gcc 1.37 and a decmips :running Ultrix 3.1, perl compiled with "gcc version 1.37.1 OSF :1.9.2.13 Ultrix Dec Mips Dec 14 1990." Shouldn't the second program :print "4" and the third be an error? The reason case 2 says what it does is that $1 as an expression is just "2+2". On the other hand , "3+$1" does yield 7, not 3+2+2, so maybe just maybe it really is a buglet, as I don't quite see why $1 shouldn't be 4 always in /e. I'd guess that the difference is one of two things: either it's us expecting a double eval when we don't deserve one, or else it's that whether $& turns into 5+7 or 12 depends on some string/numeric magic. I hope it's the former not the latter. The really unexpected thing here is that /ee yields a double eval. Here's my own test: $\ = "\n"; $orig = '5+7'; $_ = $orig; s/.*/(10 * $&) . '+7'/; print; $_ = $orig; s/.*/(10 * $&) . '+7'/e; print; $_ = $orig; s/.*/(10 * $&) . '+7'/ee; print; which gives this output: (10 * 5+7) . '+7' 50+7 57 However, if you change the test to this: $_ = $orig; s/.*/$& . '+7'/; print; $_ = $orig; s/.*/$& . '+7'/e; print; $_ = $orig; s/.*/$& . '+7'/ee; print; You get this, which is bit of a surprise: 5+7 . '+7' 5+7+7 19 I'm leery of using /ee until Larry tells us it will stick around. If he does let us use it, I'd still shy away from it, just cause it would further obfuscate something that's already obscure. (I know -- after my &id trick, you don't think that would stop me, and you'd probably be right.) But if Randal doesn't come up with a cool JAPH with it, I'll be disappointed. (nudge nudge :-) --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "With a kernel dive, all things are possible, but it sure makes it hard to look at yourself in the mirror the next morning." -me