On Mon, Feb 05, 2001 at 12:29:34PM -0500, John Porter wrote:
> James Mastros wrote:
> > OTOH, for functions that look more like {startup;
> > compute; teardown}, magic-varable is nice. Think of the functions where you
> > have a varable named $ret or somesuch, and you compute it, have another few
> > lines or few screens of code, and then say "return $ret".
>
> I see this all the time. What would fit the bill is to have something
> like a C<continue> block for subs; they get called during the same
> phase as destructors of objects going out of scope.
>
> sub readit {
> open F, "< $f" or die "$f: $!"; # even in the tiniest example
> my $l = <F>;
> close F;
> $l
> }
>
> becomes
>
> sub readit {
> open F, "< $f" ...
> scalar(<F>)
> }
> continue {
> close F;
> }
>
> O.k., the example is a little bogus, what with IO::File etc.
> But for the general case...
We can do something like this already in perl5. There's even a module
on CPAN for it: "End".
use End;
{ my $foo = end {print "Leaving the block\n"};
...
last; # Prints "Leaving the block\n".
...
}
Abigail