[OT] Should exist / does exist?

Rafael Garcia-Suarez rgarciasuarez at gmail.com
Wed Dec 6 20:45:21 GMT 2006


On 06/12/06, Andy Armstrong <andy at hexten.net> wrote:
> (I've searched CPAN for this but either I'm using the wrong search
> terms or it doesn't exist)
>
> I like using closures to emulate Ruby blocks like this:
>
> $db->withQuery(sub {
>     my $row = shift;
>     # Do stuff with $row
> });
>
> In this (hypothetical) case the anon closure would be called for
> every row in the results of a database query. This is how Ruby
> handles most of its looping constructs.
>
> This works already but it'd be even nicer to be able to write
>
> $db->withQuery(ROW => sub {
>      my $row = shift;
>      if ($row->{'some_value'} > $some_limit) {
>          last ROW;
>      }
> });
>
> In other words it'd be nice if the callback closure had something
> approaching normal loop semantics: next, redo and last would DWIM.

Interesting problem; but you don't have an iterator, here
(explicitly.) So you need to reference $db somewhere in the closure
(which will then close over $db). For example, you could use the
syntax $db->last("ROW"). (Add syntactic sugar to your taste...)


More information about the london.pm mailing list