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:
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.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');
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/)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI: when 1 != '1'
by blakem (Monsignor) on Sep 27, 2002 at 10:42 UTC | |
|
Re: DBI: when 1 != '1'
by tommyw (Hermit) on Sep 27, 2002 at 10:31 UTC | |
by rdfield (Priest) on Sep 27, 2002 at 12:51 UTC | |
by tommyw (Hermit) on Sep 27, 2002 at 14:21 UTC | |
by bart (Canon) on Sep 27, 2002 at 23:55 UTC | |
|
Re: DBI: when 1 != '1'
by diotalevi (Canon) on Sep 27, 2002 at 15:55 UTC | |
by Aristotle (Chancellor) on Sep 27, 2002 at 17:51 UTC | |
|
Re: DBI: when 1 != '1'
by thraxil (Prior) on Sep 27, 2002 at 14:43 UTC | |
|
Re: DBI: when 1 != '1'
by tadman (Prior) on Sep 28, 2002 at 00:46 UTC | |
by IlyaM (Parson) on Sep 28, 2002 at 09:56 UTC |