Hi monks,
Below is code that I have written, and want to handle DBI error so that from outside of package I can check :
If method doesn't return anything then print $obj->{ERROR_STR}
sub new { my $class = shift; my $self = bless {}, $class; my $dbh = shift || return (undef); $self->{dbh} = $dbh; # store the dbh for future use return $self; } sub getDDL { my $self = shift; my ($dbase,$tableName,$tableDtls) = @_; return (undef) if (!$dbase or !$tableName); my $tabDtlSql = qq/ ##some sql query /; my $tabDtlSth = $self->{dbh}->prepare($tabDtlSql); $tabDtlSth->execute or $self->{ERROR_STR} = "Can't execute SQL sta +tement: $DBI::errstr\n"; if ($self->{ERROR_STR}) { return (undef); } else { ## do some stuff... }
I am storing the error in ERROR_STR via hash. I have doubt:
if from other methods(other than getDDL) another sql query will get called and that fails then here $self->{ERROR_STR} will have value, and behave differently.
How should I handle the error and store so that from out side I can access error like :
$obj->{ERROR_STR} and ensure that it pronts the correct error, not from other sql query? Pls. advice.
Thanks in advance for help

In reply to Having problem with handling DBI error in object by perlCrazy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.