die'ing with the caller's line number

Discussion in 'Perl Misc' started by carloschoenberg@yahoo.com, Dec 8, 2004.

  1. Guest

    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?
    , Dec 8, 2004
    #1
    1. Advertising

  2. Guest

    I think "confess" is what you're looking for (it has backtrace).
    --Peter
    , Dec 8, 2004
    #2
    1. Advertising

  3. Guest

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

    confess "I am dying";

    --Peter
    , Dec 8, 2004
    #3
  4. Peter Scott Guest

    In article <>,
    writes:
    >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...

    --
    Peter Scott
    http://www.perldebugged.com/
    *** NEW *** http://www.perlmedic.com/
    Peter Scott, Dec 9, 2004
    #4
  5. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Dec 9, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Sheen

    Die IDE Die

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    428
    Natty Gur
    Jan 26, 2004
  2. Lloyd Sheen

    More Die IDE Die

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    447
    Alvin Bruney
    Jan 25, 2004
  3. David Abrahams

    Determining caller's file and line number

    David Abrahams, Jun 23, 2004, in forum: Python
    Replies:
    2
    Views:
    584
    David Abrahams
    Jun 24, 2004
  4. Mark
    Replies:
    2
    Views:
    391
  5. Stevens

    die-ing within functions

    Stevens, Apr 13, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    117
    Chris Mattern
    Apr 14, 2004
Loading...

Share This Page