Re: return() in pointy blocks
by Luke Palmer other posts by this author
Jun 7 2005 2:08PM messages near this date
Re: return() in pointy blocks
|
Re: return() in pointy blocks
On 6/7/05, Matt Fowles <ubermatt@[...].com> wrote:
> On 6/7/05, Ingo Blechschmidt <iblech@[...].de> wrote:
> > Hi,
> >
> > sub foo (Code $code) {
> > my $return_to_caller = -> $ret { return $ret };
> >
> > $code($return_to_caller);
> > return 23;
> > }
> >
> > sub bar (Code $return) { $return(42) }
> >
> > say foo &bar; # 42 or 23?
> >
> > I think it should output 42, as the return() in the pointy
> > block $return_to_caller affects &foo, not the pointy block.
> > To leave a pointy block, one would have to use leave(), right?
>
> I don't like this because the function bar is getting oddly
> prematurely halted.
Then let's put it this way:
sub foo () {
for 0..10 {
when 6 { return 42 }
}
return 26;
}
And if that didn't do it, then let's write it equivalently as:
sub foo () {
&map(-> $_ { return 42 }, 0..10);
return 26;
}
Do you see why the return binds to the sub rather than the pointy now?
Also, we're going to be avoiding the return continuation problem with:
sub foo() {
return -> { return 42 };
}
my $code = foo();
say "Boo!";
$code();
Says not:
Boo
Boo
Boo
...
But:
Boo
Can't return from subroutine that already returned at <eval> line 2.
Luke
Thread:
Ingo Blechschmidt
Matt Fowles
Luke Palmer
=22TSa_=28Thomas_Sandla=DF=29=22
=22TSa_=28Thomas_Sandla=DF=29=22
Larry Wall
"TSa (Thomas Sandlaß)"
Larry Wall
Matt Fowles
|