in reply to 'finally' block in Perl?
#>>> DummyDestructor.pm package DummyDestructor; use strict; use vars qw(@ISA @EXPORT); require Exporter; @ISA = qw(Exporter); @EXPORT = qw(make_finally); sub make_finally (\&) { return bless $_[0], __PACKAGE__; } sub DESTROY { $_[0]->(); } #>>> some perl script use DummyDestructor; ... { ... code ... my $final = make_finally { ... code ... } # beware! # no access to @_ in finally ... code ... }
A more straight-forward alternative is eval and a bare block:
sub f { my @return; ... more code ... # vars needed in "finally" must be # defined here! eval{{ ... and more ... # instead of return $retval you'd need: @return = $retval and last; ... yet more code ... }} ... do clean up stuff here ... die $@ if $@; return @return; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: 'finally' block in Perl?
by matvore (Initiate) on Jan 06, 2019 at 15:19 UTC | |
by haukex (Archbishop) on Jan 06, 2019 at 16:21 UTC | |
by matvore (Initiate) on Jan 08, 2019 at 15:56 UTC | |
by haukex (Archbishop) on Jan 08, 2019 at 16:46 UTC | |
|
Re: Re: 'finally' block in Perl?
by John M. Dlugosz (Monsignor) on Jul 22, 2001 at 05:38 UTC |