[OT] Should exist / does exist?

Andy Armstrong andy at hexten.net
Wed Dec 6 21:21:46 GMT 2006


On 6 Dec 2006, at 20:45, Rafael Garcia-Suarez wrote:
> 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...)

I think I may not have explained myself properly. I'd like to be able  
to make

LOOP: for (1 .. 100) {
     last LOOP if $_ > 50;
}

and

$some_sequence_thing->with(LOOP => sub {
     my $i = shift;
     last LOOP if $i > 50;
});

do the same thing.

You can fake it now in a number of ways one of which is to have  
$some_sequence_thing expose 'labels' and pass them to die like this:

$some_sequence_thing->with(sub {
     my $i = shift;
     die $some_sequence_thing->last if $i > 50;
});

$some_sequence_thing catches the die with an eval and either handles  
it or passes it on. I guess that'd work pretty well and it's arguable  
whether it really needs any more sugar. Sorry, I'm debating with  
myself here :)

And yes, I know it's not an iterator in Perlspeak but that's what a  
function that repeatedly invokes an anonymous block is called in  
Ruby. In Ruby you can do

def some_iter(max)
    i = 0
    while i < max
       yield i
    end
end

and then

some_iter(100) do |i|
    puts i
end

Having said all that it turns out (I think) that break|next|redo  
don't do anything sensible in Ruby iterators - but that doesn't mean  
I wouldn't like to be able to make them work nicely in Perl :)

-- 
Andy Armstrong, hexten.net



More information about the london.pm mailing list