Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I might be being a bit more clever than I should be, but it seemed such a good idea at the time.

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; }; }

In reply to naming anonymous subroutines inner variables by writch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-12-02 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?