die'ing with the caller's line number

  • Thread starter carloschoenberg
  • Start date
C

carloschoenberg

With DBI, it seems that if I want to use the "?" placeholders that I
must
use prepare and execute. So I made a simple helper function to do both
for
me, saving some typing and making the code slightly cleaner:

sub sql_do($@) {
my $q = shift;

my $sth = $dbh->prepare($q);
$sth->execute(@_);
}


But I run with AutoRaise on, and when I hit a fatal error, I get a line
number
inside of sql_do(), which is rarely useful.

What should I do to get the line number of the call to sql_do() when
sql_do()'s
call to execute fails?
 
P

peterkayatwork

I think "confess" is what you're looking for (it has backtrace).
--Peter
 
P

peterkayatwork

Oh, stupid on my part, forgot to mention:
use Carp;

confess "I am dying";

--Peter
 
P

Peter Scott

With DBI, it seems that if I want to use the "?" placeholders that I
must
use prepare and execute. So I made a simple helper function to do both
for
me, saving some typing and making the code slightly cleaner:

sub sql_do($@) {
my $q = shift;

my $sth = $dbh->prepare($q);
$sth->execute(@_);
}


But I run with AutoRaise on, and when I hit a fatal error, I get a line
number
inside of sql_do(), which is rarely useful.

What should I do to get the line number of the call to sql_do() when
sql_do()'s call to execute fails?

As has been pointed out, if you confess() on die, you'll get the
stack trace. If you want just the one line number output, though,
out of some desire to be neat, you could try putting sql_do() in
its own package and putting that package name in @Carp::CARP_NOT.
Note: I haven't tried this, I'm just going off documentation in
5.8 versions of Carp.pm:

Here is a more complete description of how shortmess works.
[...]
2. Packages claim that there won't be errors on calls to or from pack-
ages explicitly marked as safe by inclusion in @CARP_NOT, or (if
that array is empty) @ISA. The ability to override what @ISA says
is new in 5.8.

Or you could turn off AutoRaise inside sql_do(), do the execute,
test for an error, and croak() yourself if you find one, otherwise turn
AutoRaise back on...
 
X

xhoster

With DBI, it seems that if I want to use the "?" placeholders that I
must
use prepare and execute.

Why? What is wrong with:
$dbh->do('select something where ?=? and ?>?', undef, @bind_vars);
?

So I made a simple helper function to do both
for
me, saving some typing and making the code slightly cleaner:

sub sql_do($@) {
my $q = shift;

my $sth = $dbh->prepare($q);
$sth->execute(@_);
}

This seems like a pointless abstraction. (And it depends on the apparently
global $dbh, which is nasty).

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top