1: # This is designed to simulate END blocks using objects. This
2: # is very useful when you want to do something when scope is
3: # left.
4: #
5: # To use it, pass either the new or the add routine one or
6: # more code refs. When the object leaves scope, the subs will
7: # play. This is useful for closing files, removing temp
8: # files... all of the things you normally use END{} for
9: # But, you can call "END" any time by simply undefing the
10: # object.
11: #
12: # # you can do things like this.
13: # while(1){
14: # my $end = Pseudo::End->new( sub{ warn "Out of the loop" } );
15: #
16: # last if 0;
17: # $end->add( sub{ warn "Out of the loop2" } );
18: # last if 1;
19: # last if 0;
20: # }
21: #
22: # Simple, but try it. It is addictive.
23:
24: package Pseudo::End;
25: sub new {
26: my $type = shift;
27: my $s = bless [], ref($type) || $type;
28: foreach (@_){
29: unshift @$s, $_ if ref $_ eq 'CODE';
30: }
31: $s;
32: }
33: sub add {
34: my $s = shift;
35: foreach (@_){
36: unshift @$s, $_ if ref $_ eq 'CODE';
37: }
38: }
39: sub DESTROY {
40: my $s = shift;
41: &$_ foreach @$s;
42: }
43: 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pseudo End
by premchai21 (Curate) on Apr 24, 2001 at 22:44 UTC | |
by Rhandom (Curate) on Apr 25, 2001 at 00:22 UTC | |
by Rhandom (Curate) on Apr 25, 2001 at 00:15 UTC | |
by tilly (Archbishop) on Apr 25, 2001 at 02:18 UTC |