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

http://wholok.com/data/code/Trivia.pm
http://wholok.com/data/code/triviabot.pl

I am quite a novice with regard to perl and I am trying to get some code working.

The two links above contain code for a trivia bot to run on IRC. The questions and answers for the bot are supposed
to be stored in a database, but there is no explanation of what the structure of the database should be.

So far I have created a database named "trivia" and two tables, one called "trivia" with two fields "question" & "answer",
and the other table is called "scores" with fields named "nick" and "score".

Using the above structure, the bot can successfully send questions to a channel and accept the correct answer
and increment the score of the nick who answered correctly.

Where I am having difficulty is with the "!score" command the bot uses to display your score.

When the "!score" command is used the bot crashes with the following message

"DBI selectall_hashref: invalid number of arguments: got handle + 1, expected handle + between 2 and -1
Usage: $h->selectall_hashref($statement, $keyfield [, \%attr , @bind_params ]) at Trivia.pm line 165."

Trivia.pm line 165 = return $obj->{dbh}->selectall_hashref("SELECT * FROM scores") ;
from the get_score routine.
sub get_score { my $obj = shift; return $obj->{dbh}->selectall_hashref("SELECT * FROM scores") ; }

I don't know what line 165 means.
I am guessing it is trying to read the scores from the table but finding the format of the fields to be incorrect.

Can I get some help regarding my problem?
cheers

Replies are listed 'Best First'.
Re: Reverse engineering some code :(
by ysth (Canon) on Jun 21, 2009 at 06:39 UTC
    See the doc. selectall_hashref returns a reference to a hash where the values are hashrefs, each containing all the columns from one row, and the keys are one of the columns, which you identify with the $keyfield parameter. That call seems to be missing the $keyfield (which I'm guessing would be "nick").

    Looking at how get_score is used in triviabot.pl, it seems that it should be returning a reference to an array of hashrefs instead, which you would do like:

    return $obj->{dbh}->selectall_arrayref("SELECT * FROM scores", { S +lice => {} }) };
Re: Reverse engineering some code :(
by ceilingcat (Initiate) on Jun 21, 2009 at 06:32 UTC
    P.S. I emailed the author, but he finds that he is unable to help me.