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

I was just writing a bug off in DBD::ODBC when I needed to check the ParamTypes documentation and discovered it had a mistake:

# assuming $sth1 is a previously prepared statement handle my $sth2 = $dbh->prepare( $sth1->{Statement} ); my $ParamValues = $sth1->{ParamValues} || {}; my $ParamTypes = $sth1->{ParamTypes} || {}; $sth2->bind_param($_, $PV->{$_} $PT->{$_}) for keys %{ %$PV, %$PT }; $sth2->execute();

which should I guessed was just a typo and should be

# assuming $sth1 is a previously prepared statement handle my $sth2 = $dbh->prepare( $sth1->{Statement} ); my $ParamValues = $sth1->{ParamValues} || {}; my $ParamTypes = $sth1->{ParamTypes} || {}; $sth2->bind_param($_, $ParamValues->{$_} $ParamTypes->{$_}) for keys %{ %$ParamValues, %$ParamTypes }; $sth2->execute();

Before I committed the fix to DBI pod I thought I'd better check it out so I wrote:

use strict; use Data::Dumper; use DBI; my $h = DBI->connect; my $s = $h->prepare(q/insert into mje values(?)/); $s->bind_param(1, 2); $s->execute; my $ParamValues = $s->{ParamValues} || {}; my $ParamTypes = $s->{ParamTypes} || {}; print Dumper($ParamValues); print Dumper($ParamTypes); my $s2 = $h->prepare(q/insert into mje2 values(?)/); $s2->bind_param($_, $ParamValues->{$_}, $ParamTypes->{$_}) for keys %{ %$ParamValues, %$ParamTypes };

and it fails with "Can't use string ("1/8") as a HASH ref while "strict refs" in use at /tmp/z.pl line 18.". I've no idea why - any ideas?

The output which will make it easier to reproduce without DBI is:

$VAR1 = { '1' => 2 }; $VAR1 = { '1' => { 'TYPE' => 4 } }; Can't use string ("1/8") as a HASH ref while "strict refs" in use at / +tmp/z.pl line 18.
UPDATE: posted wrong code at first - sorry.

Replies are listed 'Best First'.
Re: Problem with hash dereferencing from DBI example
by moritz (Cardinal) on Mar 08, 2011 at 13:56 UTC
    keys %{ %$ParamValues, %$ParamTypes };

    That doesn't make any sense. %{ ... } dereferences a hash reference, but %$ParamValues, %$ParamTypes is not one.

    Maybe for (keys %$ParamValues), (keys %$ParamTypes) is meant instead?

      Thanks moritz. I got confused and was thinking the {} was turning it into a reference when in fact the {} is part of the %{}. So it would need to be %{{%$ParamValues, %$ParamTypes}} if it was to work as I thought. The 1/8 confused me - I couldn't see where the 8 came from.