[comp.lang.perl] It's not a bug...

merlyn@iwarp.intel.com (Randal L. Schwartz) (01/05/91)

In article <1991Jan3.232855.7457@NCoast.ORG>, allbery@NCoast (Brandon S. Allbery KB8JRR) writes:
| As quoted from <1991Jan03.123139.13432@convex.com> by tchrist@convex.COM (Tom Christiansen):
| +---------------
| | From the keyboard of marc@mit.edu:
| | :% perl -e '$_="|2+2|";s/\|([^\|]*)\|/$1/e;print "$_\n";'
| | :2+2
| | :% perl -e '$_="|2+2|";s/\|([^\|]*)\|/$1/ee;print "$_\n";'
| | :4
| | 
| | 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
| +---------------
| 
| It must be a bug --- otherwise, you would *need* to use /e to get $1, ... to
| expand on the RHS.  Which is not and has never been the case.

I disagree on the bug-ness.  To do "Old MacDonald" (which many people
in email have told me they enjoyed immensely, and "You're Welcome"), I
played with this one for a while, and was really unsurprised with the
consistency.

$x = "q(q-q:print q#hello#:-)";
s/.*/$x/; print "<$_>\n";
s/.*/$x/e; print "<$_>\n";
s/.*/$x/ee; print "<$_>\n";
s/.*/$x/eee; print "<$_>\n";
s/.*/$x/eeee; print "<$_>\n";
s/.*/$x/eeeee; print "<$_>\n";

which yields:

<q(q-q:print q#hello#:-)>
<q(q-q:print q#hello#:-)>
<q-q:print q#hello#:->
<q:print q#hello#:>
<print q#hello#>
hello<1>

The first subs the (null) string with the contents of $x.  So, you end
up with $_ = $x.  The second *evals* the text "$x".  The result of
eval-ing the text $x is a string consisting of the value of $x, not
the results of eval-ing the *contents* of $x.  That's done with the
third one (the 'ee' form).  And so on.

Here... this might make it clearer:

$a = 'print '; $b = '"hello"';
s/.*/$a . $b/; print "<$_>\n";
s/.*/$a . $b/e; print "<$_>\n";
s/.*/$a . $b/ee; print "<$_>\n";

which yields:

<print  . "hello">
<print "hello">
hello<1>

See... there *is* a difference.  Now go back and map that onto the
previous example to see why s/.../$1/ and s/.../$1/e are the same, and
rightfully so.

My verdict: no bug.

ObJAPH: print "Just another Perl hacker,"
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/