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

Hi all

I'm having a problem with a program that reads from one database and writes to a 2nd. I did not write it. One thing I want to see if it reads correctly from db#1. Far as I can tell, $action is an anonymous sub. I'd like to see what that is returning and then what is or is not getting pulled from the db by the fetchrow->hashref() bit.

# processes all trades which occured between start and end date, calli +ng $action for each one sub processTrades { my ($self,$action) = @_; my $count = 0; my $trdstmt = $self->_getTradesStatement(); $trdstmt->bind_param(1,$self->{'startdate'}); $trdstmt->bind_param(2,$self->{'enddate'}); $trdstmt->execute(); while(my $trade = $trdstmt->fetchrow_hashref()) { &$action($$trdstmt->fetchrow_hashref()); $count++; } return $count; }

Time flies like an arrow, fruit flies like bananas

Replies are listed 'Best First'.
Re: Dumper dereferencing problem
by bluescreen (Friar) on Jul 23, 2010 at 14:08 UTC

    First of all I think your code has a bug in the while loop on one hand you are retrieving one record and storing it in the $trade variable, on the other hand when calling $action() you are retrieving another record so you are not processing half of the records with the $action and if your query returns only one record it won't get processed but it.

    Secondly if you what to dump records I'd recommend you to use Data::Dumper, in the following form

    UNTESTED

    sub processTrades { my ($self,$action) = @_; my $count = 0; my $trdstmt = $self->_getTradesStatement(); $trdstmt->bind_param(1,$self->{'startdate'}); $trdstmt->bind_param(2,$self->{'enddate'}); $trdstmt->execute(); while(my $trade = $trdstmt->fetchrow_hashref()) { # Debug # use Data::Dumper; print STDERR Dumper($trade); # /Debug # &$action($trade); #Corrected# $count++; } return $count; }
Re: Dumper dereferencing problem
by kennethk (Abbot) on Jul 23, 2010 at 14:04 UTC
    Obviously, more info is always helpful in dissecting code, but here are a few general comments:
    • As evidenced by the call &$action($$trdstmt->fetchrow_hashref());, $action is a code ref, though I'd need to see more code to determine is it is an anonymous sub. I'd also have to see more code to know what it does.
    • Based on the structure, I'm guessing that $trade in the while-loop contains your database return. A simple way to examine its contents should be to use Data::Dumper, a la:
      while(my $trade = $trdstmt->fetchrow_hashref()) { print Dumper $trade; &$action(%$trade); $count++; }
      after adding use Data::Dumper; to your code.
    • I am guessing based on syntax that $self is a DBI database handle. Assuming this is the case, I would suggest instrumenting to test for success on your database calls. Perhaps something like:
      $trdstmt->bind_param(1,$self->{'startdate'}) or die "Bind failed: +$DBI::errstr"; $trdstmt->bind_param(2,$self->{'enddate'}) or die "Bind failed: $D +BI::errstr"; $trdstmt->execute() or die "Execute: $DBI::errstr";
    • For a reasonable review of Perl approaches to tackle this task, read Basic debugging checklist.

    Update: Note the bug caught by bluescreen below.

Re: Dumper dereferencing problem
by roboticus (Chancellor) on Jul 23, 2010 at 15:52 UTC

    hsweet:

    Run the program in debug mode, set a breakpoint before the call, and then step through it. Then you'll be able to find out which subroutine is executing and examine the data you want. It's pretty simple, really:

    roboticus@Boink> # Execute script under debugger: roboticus@Boink> perl -d genfiles.pl Loading DB routines from perl5db.pl version 1.32 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(genfiles.pl:5): for (1 ... 50000) { DB<1> # Set breakpoint on line 7 DB<2> b 7 DB<3> # Execute (continue) until end or breakpoint DB<4> c main::(genfiles.pl:7): my $u = join('',('A'..'Z')[rand 26, rand + 26, rand 26]); DB<4>

    As the debugger startup message says, use 'h' for help.

    ...roboticus

    Time flies like an arrow. Fruit flies like a banana. Groucho Marx