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

I set a DBI error handler 'handle_error' with $dbh->{RaiseError} = 1; and I want to inspect the arguments passed to it under the debugger with perl -d
sub handle_error { my $errortext = shift; my $handle = shift; my $dbh; my $dbh = $handle->{'Database'};
when I run it under the debugger when inside 'handle_error' I get
DB<2> x @_ 0 'Duplicate key on INSERT detected. (Tue Jan 30 08:45:55 2024)' 1 DBI::db=HASH(0x33c6af0) empty hash 2 undef
However DBI::db=HASH(0x33c6af0) cannot be empty since I consume it in the next lines with
my $dbh = $handle->{'Database'};

Why does the debugger say that it is empty, and how can I inspect
the contents of DBI::db=HASH(0x33c6af0) ?

Replies are listed 'Best First'.
Re: DBI handle_error debugging (updated)
by LanX (Saint) on Jan 30, 2024 at 07:43 UTC
    This is guesswork, because I couldn't find much documentation on db ...

    But it seems to be provided by the DBD (database driver) for the SQL server you use.

    Those are normally implemented in XS resp. C, and their data structures can be magic.

    Much like a Tie::Hash where FETCH is provided but not FIRSTKEY and NEXTKEY.

    Consequently without inspection of the keys, one can't dump it.

    For that one needs to know beforehand, which keys are accepted by FETCH.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    update

    To avoid confusion, this says that you are dealing with an object not a simple hash. To be precise a hash blessed into the package DBI::db. The rest holds.

    1 DBI::db=HASH(0x33c6af0) empty hash

    But you might want to check it again with Data::Dumper to rule out problems with the debuggers internal dumper.

      Is there a way to display the contents of the object (DBI::db=HASH(0x33c6af0)) under the debugger when I don't have a handle to it?
        You can loop over the keys/attributes you know to exist.

        First step would be to find the documentation of DBI::db...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery