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

Hello all.

I'm attempting to pre-build part of a SQL statement in perl before passing it to the DBI engine. My SQL statement needs to do a like in the DB engine, and I'd intend to utilize the SQL % sign. However, the presence of the % sign in the string has perl attempt to satisfy the %B or %1 as the string gets assigned instead of just passing it along as a string.

I'd like my SQL statement to eventually resemble the following:

select winner from results where round='2' and (event like '%USA%' and + event like '%Boys%');

here's a code snippet example of the perl I'm attempting to write:

if ($form{'category'} eq 'All USA Boys') { $clause="(event like '%USA%' and event like '%Boys%')"; } printf "Existing clause assignment logic; clause variable is $clause<p +>" if $debug;

The printf of the above code ends up as follows:
Existing clause assignment logic; clause variable is (event like '0SA%' and event like '0oys%')

%U got turned into 0, as did %B. This also happens with a few other words where I attempt to wrap the word in a like statement.

How do I get Perl to NOT interpret this and to pass the string along as intended?

I've tried escaping the % sign with a \, tried \Q and \E, tried replacing the % sign as a variable, tried breaking up the strings at the % signs and then re-concatenating them, etc. All to no avail.

Hoping i'm missing something simple...thanks in advance.

Replies are listed 'Best First'.
Re: Escaping % sign in a like string bound for SQL
by LanX (Saint) on Nov 12, 2018 at 21:56 UTC
    why printf and not print?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Ugh. You're right; printf was the culpret. print showed it properly formatted ... and as it turned out, the SQL was being generated and passed correctly to the engine. My printf diagnostic commands were what was wrong.

      Ok, my bad, no issue. apologies.

        This happens to all of us.

        You're welcome! :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice