Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

special $dbh can't error?

by themage (Friar)
on Oct 03, 2008 at 10:20 UTC ( #715176=perlquestion: print w/ replies, xml ) Need Help??
themage has asked for the wisdom of the Perl Monks concerning the following question:

(re)Blessed minds,

Imagine the following code:
use DBI; my $dbh=DBI->connect("dbi:mysql:hostname=localhost;database=somedb","u +serok","passwdok"); bless $dbh, "My::Special::DBI::db"; print "can error: ", $dbh->can("error"),"\n"; package My::Special::DBI::db; use base qw(DBI::db); sub error { my $dbh=shift; return $dbh->errstr(); }
I would expect that in this code, the $dbh->can("error") returned a reference to My::Special::DBI::db::error.

However, undef is returned.

Can anyone explain me what sets my expections out of sync with reality?

For extra-information, I tested this with:

perl -v This is perl, v5.8.7 built for x86_64-linux-gnu-thread-multi perl -v This is perl, v5.10.0 built for i486-linux-gnu-thread-multi
Thank you, everyone.

Comment on special $dbh can't error?
Select or Download Code
Replies are listed 'Best First'.
Re: special $dbh can't error?
by Anonymous Monk on Oct 03, 2008 at 10:43 UTC
    Can anyone explain me what sets my expections out of sync with reality?
    Probably because can isn't what you think it is. Try UNIVERSAL::can()
    use DBI; my $dbh=DBI->connect("dbi:SQLite:dbname=dbfile"); warn $dbh; print "can error: ", $dbh->can("error"),"\n"; print "UNIVERSAL::can((", UNIVERSAL::can($dbh,'error'),"\n"; eval {print $dbh->error('eeep');};warn $@ if $@; bless $dbh, "My::Special::DBI::db"; warn $dbh; print "can error: ", $dbh->can("error"),"\n"; print "UNIVERSAL::can((", UNIVERSAL::can($dbh,'error'),"\n"; eval {print $dbh->error('eeep');};warn $@ if $@; print "\n-----\n\n"; package My::Special::DBI::db; use base qw(DBI::db); sub error { my $dbh=shift; return $dbh->errstr(); } __END__ DBI::db=HASH(0x19b7f1c) at dbi.dbd.error.subclass.pl line 3. can error: UNIVERSAL::can(( Can't locate object method "error" via package "DBI::db" at dbi.dbd.er +ror.subclass.pl line 6. My::Special::DBI::db=HASH(0x19b7f1c) at dbi.dbd.error.subclass.pl line + 8. can error: UNIVERSAL::can((CODE(0x19978e4) -----
Re: special $dbh can't error?
by jethro (Monsignor) on Oct 03, 2008 at 12:30 UTC

    It seems 'can' is overriden by DBI and for it to know your method you would have to override it in turn.

Re: special $dbh can't error?
by massa (Hermit) on Oct 03, 2008 at 15:32 UTC
    base.pm seems to have a bug; if you use
    push @ISA, qw(DBI::db);
    instead of
    use base qw(DBI::db);
    the thing starts to work...
    []s, HTH, Massa (κς,πμ,πλ)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://715176]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2015-07-28 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (253 votes), past polls