The quote() method is database dependent.
I made this quick test for a MySQL database, and I got
the correct result with both trace(2) and trace(0).
If I change 'joe' into undef, then I get NULL in both cases.
#!/usr/bin/perl -w
use DBI;
use strict;
my $hDB = DBI->connect("DBI:mysql:test;host=localhost"
. ";mysql_read_default_file=$ENV{HOME}/.my.cnf",
undef, undef, {RaiseError => 1})
or die "can't connect\n";
my $Vars = { 'userid' => 'joe', 'password' => 'secret'};
DBI->trace ( 0 );
my $UserID = $hDB->quote ( $Vars->{ userid } );
my $Password = $hDB->quote ( $Vars->{ password } );
DBI->trace ( 0 );
print "$UserID $Password\n";
$hDB->disconnect();
__END__
'joe' 'secret'
Could you make a similar case script? The culprit could be something else.
quote() will return NULL when your value is undef. I suspect that your values are either undef or becoming undefined somehow along the processing flow.
update
After talexb's example, I was able to reproduce the problem and indeed it seems that only by stirring the variable you can get the wanted result.
One of my favorite idioms for quoting (when I am not using placeholders) is to map-quote the variables from an array.
The following example gives the right result, although it only works with the interpolated "$_".
my ($UserID, $Password) = map {$hDB->quote( "$_" )}
($Vars->{userid}, $Vars->{password});
update (2)
Also a simple interpolation without map tricks will do the job.
my $UserID = $hDB->quote ( "$Vars->{'userid'}" );
my $Password = $hDB->quote ( "$Vars->{'password'}" );
It solves the problem, but doesn't satisfy my curiosity ... ;)
update (3)
Although the quote() method is database dependent, in this case it is not, since the responsible for this strange return is before any other method could be called.
# from the DBI.pm module
1126 sub quote {
1127 my ($dbh, $str, $data_type) = @_;
1128
1129 return "NULL" unless defined $str;
# more code follows .....
This code will return NULL if the string passed is undefined. However, the string is defined, as
I could check with a simple print.
Just brainstorming ...
_ _ _ _
(_|| | |(_|><
_|
|