writch has asked for the wisdom of the Perl Monks concerning the following question:
I have some heavily query driven code. I intended to use a hash of SQL queries to build out a number of prepared queries. That part is working fine.
Then, I realized, rather than write functions to return the result sets, I maybe could, in the same loop which is doing the preparation of the queries, make an anonymous subroutine and assign it, rather than writing it by hand.
The problem is in the sixth line. When I do my 'test' query, $self->{test} is the prepared query, and $self->{ftest}() is the code that would execute it.
But the $_ that turned out fine in the earlier uses is useless in the code itself.
The error is 'Can't call method "execute" on an undefined value' because (of course) the line is actaully $self->{}->execute(@_) instead of $self->{test}->execute(@_), as $_ isn't populated when it's being executed, just when it's being created.
for (keys %{$self->{SQL}}) { $self->{$_} = $self->{DBO}->prepare($self->{SQL}->{$_}); $self->{"f$_"} = sub { my $self = shift; my @res; $self->{$_}->execute(@_); while (my $dbrow = $self->{$_}->fetchrow_h +ashref()) { push @res, \$dbrow; } return \@res; }; }