[OT] Should exist / does exist?

Paul Johnson paul at pjcj.net
Wed Dec 6 21:36:27 GMT 2006


On Wed, Dec 06, 2006 at 08:33:49PM +0000, Andy Armstrong wrote:

> $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.

> You can fake it at the moment using die / eval and could probably  
> implement it properly with either a source filter or by decompiling  
> and munging the closure.
> 
> Before I have a play with it has anyone already been this way?

#!/usr/bin/perl

use strict;
use warnings;
no warnings "exiting";

sub X::withQuery
{
    my $self = shift;
    my ($loop, $sub) = @_;
    eval <<EOE;
    $loop:
    for my \$l (1 .. 3)  # or whatever your loop looks like
    {
        \$sub->(\$l);
    }
EOE
    die "argh: $@" if $@;
}

my $db = bless {}, "X";

$db->withQuery(ROW => sub {
    my $row = shift;
    print "-- $row\n";
    if ($row > 1) {
        last ROW;
    }
});


-- 
Paul Johnson - paul at pjcj.net
http://www.pjcj.net


More information about the london.pm mailing list