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