Skip Menu |

This queue is for tickets about the DBI CPAN distribution.

Report information
The Basics
Id: 79492
Status: rejected
Priority: 0/
Queue: DBI

People
Owner: Nobody in particular
Requestors: freemat [...] free.fr
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.609
Fixed in: (no value)



Subject: private_* undef with execute ?
Hi, I'm using DBI 1.609 and perl v5.10.1 on Linux 2.6.35-25-generic #44~lucid1-Ubuntu SMP Tue Jan 25 19:17:25 UTC 2011 x86_64 GNU/Linux I wrote a custom function to handle errors, and I store values in a private attribute ("private_mymodule_value") When a dbh->do(...) throws an error, I can access to my private value in my handle_error function. But when $sth->execute throws an error, my private value is undef. An example : " #! /usr/bin/perl -w use strict; use DBI; my $dbm_str = 'DBI:mysql:xxxx'; my $dbm_login = 'xxxx'; my $dbm_pass = 'xxxx'; my $dbh = DBI->connect($dbm_str, $dbm_login, $dbm_pass, { RaiseError => 1, PrintError => 0 }) || die; $dbh->{private_mymodule_value} = '42'; $dbh->{HandleError} = \&handle_error; #$dbh->do('bad request'); my $sth = $dbh->prepare('bad_request'); $sth->execute; sub handle_error { my ($error, $db_handle, $str) = @_; print "Entering custom handle_error_function\n"; print 'Value = [' . $db_handle->{private_mymodule_value} . "]\n" ; } " The output is : " Entering custom handle_error_function Use of uninitialized value in concatenation (.) or string at ./test_dbi.pl line 26. Value = [] " With dbh->do("bad_request"), the output is: " Entering custom handle_error_function Value = [42] " Am I doing something wrong ?
On Thu Sep 06 18:57:50 2012, freemat@free.fr wrote: Show quoted text
> Hi, > > I'm using DBI 1.609 and perl v5.10.1 on Linux 2.6.35-25-generic > #44~lucid1-Ubuntu SMP Tue Jan 25 19:17:25 UTC 2011 x86_64 GNU/Linux > > I wrote a custom function to handle errors, and I store values in a > private attribute ("private_mymodule_value") > > When a dbh->do(...) throws an error, I can access to my private value in > my handle_error function. > > But when $sth->execute throws an error, my private value is undef. > > An example : > > " > #! /usr/bin/perl -w > > use strict; > use DBI; > > my $dbm_str = 'DBI:mysql:xxxx'; > my $dbm_login = 'xxxx'; > my $dbm_pass = 'xxxx'; > > my $dbh = DBI->connect($dbm_str, $dbm_login, $dbm_pass, { RaiseError => > 1, PrintError => 0 }) || die; > $dbh->{private_mymodule_value} = '42';
you stored 42 in private_mymodule_value in the connection handle. Show quoted text
> $dbh->{HandleError} = \&handle_error; > > #$dbh->do('bad request'); > > my $sth = $dbh->prepare('bad_request'); > $sth->execute; > > sub handle_error { > my ($error, $db_handle, $str) = @_;
that $db_handle is a connection handle in the first case and a statement handle in the second case. If you want the private attribute you need to get the connection handle from the statement handle. See http://search.cpan.org/~timb/DBI-1.622/DBI.pm#Type to find out the type of a handle. See http://search.cpan.org/~timb/DBI-1.622/DBI.pm#Database to find a connection handle from a statement handle. Show quoted text
> print "Entering custom handle_error_function\n"; > print 'Value = [' . $db_handle->{private_mymodule_value} . "]\n" ; > } > " > > The output is : > > " > Entering custom handle_error_function > Use of uninitialized value in concatenation (.) or string at > ./test_dbi.pl line 26. > Value = [] > " > > With dbh->do("bad_request"), the output is: > > " > Entering custom handle_error_function > Value = [42] > " > > Am I doing something wrong ? >
See above. Martin -- Martin J. Evans Wetherby, UK
From: freemat [...] free.fr
Many thanks, it works ! I have a last question. I want to catch connection errors, so I wrote this code: " my $dbh = DBI->connect($dbm_str, $dbm_login.'_bad', $dbm_pass, { RaiseError => 1, PrintError => 0, private_mymodule_value => 42, HandleError => \&handle_error }) || die; sub handle_error { my ($error, $db_handle, $str) = @_; my $dbh; if( $db_handle->{Type} eq 'st' ) { $dbh = $db_handle->{Database} ; } elsif( $db_handle->{Type} eq 'db' ) { $dbh = $db_handle ; } elsif( $db_handle->{Type} eq 'dr') { .... } print "Entering custom handle_error_function\n"; print 'Value = [' . $dbh->{private_mymodule_value} . "]\n" ; } " How can I get my "private_mymodule_value" from a 'dr' type ? I have read the documentation, but I can't find the answer for this particular point.
On Fri Sep 07 07:27:25 2012, freemat@free.fr wrote: Show quoted text
> > Many thanks, it works ! I have a last question. I want to catch > connection errors, so I wrote this code: > > " > my $dbh = DBI->connect($dbm_str, $dbm_login.'_bad', $dbm_pass, { > RaiseError => 1, PrintError => 0, private_mymodule_value => 42, > HandleError => \&handle_error }) || die; > > sub handle_error { > > my ($error, $db_handle, $str) = @_; > > my $dbh; > > if( $db_handle->{Type} eq 'st' ) { > $dbh = $db_handle->{Database} ; > } elsif( $db_handle->{Type} eq 'db' ) { > $dbh = $db_handle ; > } elsif( $db_handle->{Type} eq 'dr') { > > .... > > } > > print "Entering custom handle_error_function\n"; > print 'Value = [' . $dbh->{private_mymodule_value} . "]\n" ; > > } > " > > How can I get my "private_mymodule_value" from a 'dr' type ? > > I have read the documentation, but I can't find the answer for this > particular point. > >
I don't think it is meaningful to do that since a connection handle probably does not exist until the connection has succeeded. For the future you might be better subscribing to the dbi-users list and posting there first - see http://dbi.perl.org and click on support. Martin -- Martin J. Evans Wetherby, UK
Closing this now. Thanks.