in reply to Re^2: Regexp and metacharacters
in thread Regexp and metacharacters
I have random data coming in from many XML files which are being parsed and stuffed into a hashThis vastly narrows things down, thanks (and also makes clear that quotemeta|\Q\E is not what you should be doing...)
Much still depends on where your quoted strings are really coming from, and, at this point, I see two possibilities (yell if it's not one of these):
As for getting stuff into databases, you were right the first time: $dbh->quote is the right way to insert an arbitrary string value into an SQL statement — that regexp quote happened to work for you is more a matter of luck that both Perl regexp and (your database's version of) SQL (apparently) use backslashes in the same way (most of the time, except for those cases where they don't, which you won't find out about until stuff breaks...).
But actually a better way to do this is to use parameterized queries, if you can. For example, instead of
$dbh->do('INSERT INTO mytab VALUES('. $dbh->quote($value) .', ...);');
do
$dbh->do('INSERT INTO mytab VALUES (?, ...);', {}, $value, ...);
Granted, you'll need to check what format for parameter placeholders your driver will accept ("?" is supposed to be universal, except where it isn't. I believe MSFT uses something else, but I forget...). And if your driver does not grok parameters, you may be able to choose another one that does (e.g., there may be an ODBC-based driver for your database...).
While you didn't say what sorts of things your current setup was burping on, and while my current bet would be on the homegrown XML parser screwing up character entities or CDATA stuff, there is also the (perhaps remote, but maybe not) possibility that $dbh->quote isn't doing quite the right thing for your database's version of SQL. The point being that parameterized queries leave it up to the database driver to implement the quoting, and since the driver is specific to your database, it's a lot more likely to get the quoting right (i.e., if there are any dangerous corners in your database's version of SQL that DBI.pm doesn't know about).
Bottom line here being that if you (1) use a proper XML parser and (2) have a reasonable database driver, you should not be having to do any quote-stripping or escaping/unescaping at all.
|
|---|