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

I've written a fair amount of Perl back in the day, but nothing OO. Now I'm forced to use a callback, and don't have a clue how despite assorted research. Here's the code:
sub itm { } my $db = SQLite::DB->new($DBfile); + $db->connect or croak "Can't connect to $DBfile, aborting\n"; + $db->select("select * from item where date = $date", \&itm) or croak " +Can't read items for '$date', aborting\n";
Now I presume that somehow the data from the selected rows will be handed to my itm() callback function, but I don't know how. How can I access this data?

Replies are listed 'Best First'.
Re: callback function params
by Corion (Patriarch) on May 25, 2024 at 16:20 UTC

    If you still can, consider using DBI together with DBD::SQLite instead. It is more commonly used and does not tie your application to SQLite.

    Looking at the documentation of DB::SQLite, it seems that you are supposed to give it a callback like the following:

    $db->select( $sql, sub { my ($sth) = @_; use Data::Dumper; print "I received the following statement handle: "; print Dumper $sth->fetchall_arrayref; });

    Update: Fixed syntax errors in code

      Thanks for the suggestion. Personally, I'd prefer to avoid DBD::SQLite, as it provides gobs more functionality than I need. For additional information, when I invoke Dumper(@_) in my callback subroutine, I get:
      $VAR1 = bless( {}, 'DBI::st' );
      When I try the proffered code, I get: Can't call method "fetchall_arrayref" on an undefined value.
        That looks like you omitted the my ($sth) = @_; line.
        Ooops! Found my problem. I simply didn't have $date in quotes, as SQL requires. Thanks for the help.