I am pretty adept at Perl, and I had trouble deciphering this one -- Perl Angst. The first line (@fields = (0, @fields); threw me for a loop as in "why the... what the... well, that's weird." But, I read on assuming it would make more sense once I got to the applicable part.
while ($r = $q->fetchrow_arrayref) { Well, that's a common idiom. That made sense, $q is obviously a DBI statement handle, and he is fetching array references from it into $r and looping over the set. Any Perl coder has seen that more than once. And, the next line (my $topic = $r->[0];)) makes perfect sense, too. But, then there's the next line...while ($topic != -1) {That could really use a comment. Why negative one? Why is it in a while loop? It becomes apparent, later, that $topic gets reassigned and -1 is a sentinal value in the result set from the DB, but a quick comment would help put it in context.
The next bit is just some Perl-fu:
foreach $i (1 .. $#{$r})
{
$totals{$fields[$i]}->[$topic] += $r->[$i];
}
$topic = $parents[$topic];
So, $i is spanning over the length of @$r, but it is being used to index both @$r and @fields. That seems a little awry, but we can assume that @fields was populated with a call to $q->{NAME}->() or such. But, still, it's a terse line. And, where did @parents come from, anyways?
Well, like he said at the end
Now I’ll get email from dozens of Perl gods explaining...
So, I am not going to do that. Rather, I am just going to reiterate his conclusion
[Perl] gets the job done in a hurry... But, you might hate yourself in the morning.
I've had mornings like that.
J$
$a="A"and$'_="J";map{$a++}(66..ord
);$$_='$';print$'a,$$a
Comments