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

I'm trying match the following: '$ (a literal apostrophe followed by a literal dollar sign).

This is my attempt:

if ($source =~ /'\$/s) { push @errors, "possible unescaped sql string"; }
...and it doesn't work. Trawling through man perlre mentions this but I don't understand the solution:
You cannot include a literal $ or @ within a \Q sequence. An unescaped $ or @ interpolates the corresponding variable, while escaping will cause the literal string \$ to be matched. You'll need to write something like m/\Quser\E\@\Qhost/.
What is the secret?

Thanks
Steve Woodcock

Replies are listed 'Best First'.
RE: How can I match '$ (literally)
by merlyn (Sage) on Aug 02, 2000 at 17:13 UTC
    Just going on a hunch, but with the words "unescaped sql string" in your error message, I'm wondering if you are aware of the quoting facilities of DBI? perldoc DBI should give you more information. If you're not using DBI for your SQL access yet, you should really look into it.

    -- Randal L. Schwartz, Perl hacker

Re: How can I match '$ (literally)
by agoth (Chaplain) on Aug 02, 2000 at 15:35 UTC
    Maybe Ive misunderstood:

    $var = "'\$"; print $var, "\n"; print "wahoo\n" if ($var =~ /\'\$/);

    I notice your output mentions sql strings, have you tried using the $dbh->quote method? may be the answer to your problems?

      Oops. I'm an idiot. I thought it was matching every file but... it wasn't.

      Thanks for your help.

RE: How can I match '$ (literally)
by mrmick (Curate) on Aug 02, 2000 at 18:10 UTC
    One thing I have found in using regex's in Perl is that if I am unsure whether a character should be escaped to get the literal character, I could escape it anyway.

    For example: I have escaped ' " $ | because I was not quite sure about matching the literal character and didn't want to take chances that the regex didn't work. This may be cheating but also in the spirit of programming Perl (IMHO).

    Mick