Funnily enough nearly all my meditations are inspired by very weird bugs I've found in my or others code. Today I found that code which worked on my home computer just fine didn't work in production. An the fix was simple as replacing 1 with '1'. Now it would take long time to describe the mechanics of that bug but I would like to share with other monks my knowledge about situations when 1 != '1' in Perl.

One of the first things Perl newbies learn is that there is very little difference between numbers and strings in Perl. In most cases you can use 'NNN' and NNN (where NNN is a number) interchangeably in your Perl code. But concerning Perl internals there is a difference: it stores numbers and strings differently and it is possible to distinguish them on the level of internals. Moreover there exist some tricks to check if variable contains a number or a string without resorting to writing C code.

Still usually Perl programmers do not see any difference between numbers and strings but there is very important exception - DBI. When you use placeholders in DBI types of variables may affects how it generates SQL queries. Simple example:

my $sth = $dbh->prepare('SELECT name FROM user WHERE id = ?'); # generates query "SELECT name FROM user WHERE id = 1" $sth->execute(1); # generates query "SELECT name FROM user WHERE id = '1'" $sth->execute('1');
Well, with most databases and with most field types these two queries are treated as same. However it is not always true - for example if our database is MySQL and field 'id' has 'enum' type these two queries are very different.

Another (a bit MySQL specific) example when the difference between 1 and '1' is critical:

my $sth = $dbh->prepare('SELECT name FROM user LIMIT ?'); # generates query "SELECT name FROM user LIMIT 1" $sth->execute(1); # generates ILLEGAL query "SELECT name FROM user LIMIT '1'" $sth->execute('1');

--
Ilya Martynov (http://martynov.org/)


In reply to DBI: when 1 != '1' by IlyaM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.