Subject: | functions with state |
Date: | Sun, 25 Mar 2012 19:55:09 -0600 |
To: | bug-DBD-SQLite [...] rt.cpan.org |
From: | Luis Mochan <mochan [...] fis.unam.mx> |
I'm not sure this is a bug, but it certainly was problematic and took
me a long time to sort out, and I couldn't find documentation on the
following behavior. In order to obtain the first $N rows for each
value of a given identifier id, I used statements like
my %counters;
$dbh->func("mycount", 1, sub {++$counters{$_[0]}}, "create_function");
my $results=$dbh->selectall_arrayref(
"SELECT mycount(id) AS cid, id, moreInfo WHERE cid<=$N"
);
My idea was to hold in $counters{firstid}, $conters{secondid},... the
number of times that 'firstid´, 'secondid' ... had appeared during the
search, using the hash %counters as an array of counters to keep the
state during the 'select'. The problem is that I obtained less rows than
expected. Curiously, changing the where clause to
WHERE cid<=$N AND cid<=$N
I obtained an even smaller number of rows, as if the first comparison
differed from the second one. I found out that the reason
for these odd (at least, surprising, IMHO) behaviors is that each time
I use 'cid' the function
sub {++$counters{$_[0]} }
was called. I expected to use 'cid' as if it were a variable holding
the result of the function, and didn't expect it to work as a macro
expanssion. Is this a bug or was my expectation wrong? Is there a way
of holding computed values as if they were variables without re-doing
the computation?
Best regards,
Luis
--
o
W. Luis Mochán, | tel:(52)(777)329-1734 /<(*)
Instituto de Ciencias Físicas, UNAM | fax:(52)(777)317-5388 `>/ /\
Apdo. Postal 48-3, 62251 | (*)/\/ \
Cuernavaca, Morelos, México | mochan@fis.unam.mx /\_/\__/
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org