in reply to Re^2: Regex Grumblings (Variable Interpolation)
in thread Regex Grumblings (Variable Interpolation)

But even better, than I tried some deliberate typos, to check for funny things. And guess what, I found something funny!
$_='Some string with $foo and bar'; $bar='bar'; $foo='$bar'; /$bar/ and print "$&\n"; /$foo/ and print "$&\n"; /\Q$fooE/ and print "$&\n"; s/\Q$fooE/XYZ/; print "$_\n";
Prints bar twice, and replaces bar by XYZ! This is definitely very strange... is this a bug or what?

Jeroen
"We are not alone"(FZ)
Update: This was run with perl5.6/linux: "This is perl, v5.6.0 built for i386-linux "
(2) grinder I made this typo on purpose.
(3) Thx grinder, things are as they should be now :-)

Replies are listed 'Best First'.
Re:x4 Regex Grumblings (Variable Interpolation)
by grinder (Bishop) on May 23, 2001 at 16:15 UTC

    Don't you mean

    /\Q$foo\E/ and print "$&\n"; s/\Q$foo\E/XYZ/;

    Using -w would have helped you figure it out.

    Update: oh right, deliberate tyops. Well, now that I've really thought about it... this is what is going on. The line

    /\Q$fooE/ and print "$&\n";

    given that $fooE is not defined is the same thing as

    /\Q/ and print "$&\n";

    which is the same thing as

    // and print "$&\n";

    Which is the same thing as finding the same thing as the last successful match. This also applies to the s expression, which is why your bar appears to be magically transmuting itself into XYZ.

    That's not a bug, that's a feature.


    --
    g r i n d e r