dash2 has asked for the wisdom of the Perl Monks concerning the following question:

Didn't quite know where to put this.

[dash2@davehj plugins]$ perl $foo='3$34£$'; $test = '3'; $foo =~ s/$test//g; print $foo; $4£$ [dash2@davehj plugins]$ perl $foo='3$34£$'; $test = '$'; $foo =~ s/$test//g; print $foo; 3$34£$
Is it a bug? Is it just in 5.6?

cheers

dave hj~

Replies are listed 'Best First'.
(tye)Re: re bug?
by tye (Sage) on Apr 18, 2001 at 18:14 UTC

    If you use a variable in a regex, it is interpretted as a regex unless you tell the regex that you want it to just be a string. This is a very common mistake. s/\Q$test\E//g; is how you tell the regex that.

            - tye (but my friends call me "Tye")
Re: re bug?
by japhy (Canon) on Apr 18, 2001 at 18:12 UTC
    The $ character is a special regex character. You need to backslash it if you mean to match it specifically:
    $re = '\$'; # or, better yet: $re = qr/\$/;


    japhy -- Perl and Regex Hacker
Re: re bug?
by Albannach (Monsignor) on Apr 18, 2001 at 18:14 UTC
    No bug, it's just that s/// interpolates, and '$' has a special meaning on the left side of s///. Try using $test = '\$' and see what happens.

    --
    I'd like to be able to assign to an luser