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

Context:
Saving perl code into a database (mySQL)

Issue:
When storing the code, I use quotemeta to prepare for the insert. Problem arises with the % symbol. Every time I store the code it adds a new \ to any % symbol.

Great feature if I wanted to see how often the code is accessed - BUT I DON'T :-(

Apology:
I really have tried to RTFM (more than once) ... but age and illegal substances have taken their toll.

janitored by ybiC: Retitle from "quotemeta %" for better search results

  • Comment on quotemeta and \% to insert perl code into database

Replies are listed 'Best First'.
Re: quotemeta %
by Abigail-II (Bishop) on Mar 12, 2004 at 13:24 UTC
    Well, '%' is a non-word character, so quotemeta will escape it. But why are you using quotemeta to prepare for an insert? Why not use place holders, or the 'quote' function from DBI?

    Abigail

      Abigail,

      As usual, you have given a neat solution - a big thanks !


      (Footnote:
      it still doesn't explain why this issue arises ...
      I should have stated that there are lots of other non-word characters to be inserted in the blob. They are all handled perfectly - i.e. the DBI clears the escapes upon insertion so the next retrieval displays the contents without all escape the characters.

      One to note for the future, I guess.)
        The reason is that quotemeta is used to escape strings for regular expressions and $dbh->quote is used to escape strings for a particular database. For most databases, the only characters that need to be escaped are quotes. Regular expression have many more characters that need to be escaped. My guess is that the database does not unescape some of the escape characters.
        print $dbh->quote("foo\"%!bar"); print quotemeta("foo\"%!bar");
        Prints:
        'foo%"!bar' foo\"\%\!bar
Re: quotemeta %
by matija (Priest) on Mar 12, 2004 at 13:30 UTC
    You shouldn't quotemeta already quotemeta-ed strings. There is no reasonable way for quotemeta to be able to tell what has been quotemeta-ed before, and what just needs a backslash before the special characters because you put it there.

    Data going back-and-forth should be marked somehow, so that you are able to tell that quoting has already been done.

    You can write your own routine that won't escape backslashes that are in front of characters that need escaping, but I predict sooner or later you will run accross a string where the backslash came in the input, and not as the result of quoting.

      Matija,

      as per my response to Abigail, I guess I didn't include enough info (sorry). The DBI stores the info UN-quotemeta'ed(!). So the next retrieval displays as it was originally.

      I tested this for all characters from chr(34) to chr(255) and the only one that doesn't get UN-quotemeta'ed is the % Symbol.


      Actually I've just realised (duh) that the issue is not with "quotemeta" at all, but the DBI which strips all other escapes out but the one preceding the %.

      Anyway, thanks for your help.
Re: quotemeta %
by chip (Curate) on Mar 12, 2004 at 17:44 UTC
    As Zathras would say: "This is wrong tool."

    Don't use quotemeta or quote or any other such thing. Use "?" placeholders. No chocolate quoting mess!

        -- Chip Salzenberg, Free-Floating Agent of Chaos