line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright (C) 1997 Philippe Verdret |
2
|
|
|
|
|
|
|
require 5.000; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Hook::PrePostCall; |
5
|
1
|
|
|
1
|
|
590
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
456
|
|
6
|
|
|
|
|
|
|
$Hook::PrePostCall::VERSION = '1.2'; |
7
|
|
|
|
|
|
|
sub new { |
8
|
3
|
|
|
3
|
0
|
102
|
my $receiver = shift; |
9
|
3
|
|
|
|
|
5
|
my $class = ''; |
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
|
|
5
|
my $callfrom = ''; |
12
|
3
|
|
|
|
|
4
|
my $self = ''; |
13
|
3
|
|
|
|
|
4
|
my $primary = ''; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# The primary routine |
16
|
3
|
|
|
|
|
4
|
my $name = shift; |
17
|
3
|
50
|
|
|
|
10
|
if ($name =~ /^(.*)::/) { |
18
|
0
|
|
0
|
|
|
0
|
$callfrom = ($1 or 'main'); |
19
|
|
|
|
|
|
|
} else { |
20
|
3
|
|
|
|
|
22
|
$callfrom = (caller(0))[0]; |
21
|
3
|
|
|
|
|
9
|
$name = "${callfrom}::" . $name; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
3
|
50
|
|
|
|
10
|
if ($class = ref $receiver) { # Object method |
25
|
0
|
|
|
|
|
0
|
$self = $receiver; |
26
|
0
|
|
|
|
|
0
|
$primary = ${$self->{primary}}; # initial definition |
|
0
|
|
|
|
|
0
|
|
27
|
|
|
|
|
|
|
} else { # Class method |
28
|
3
|
|
|
|
|
9
|
$self = bless {}, $receiver; |
29
|
3
|
|
|
|
|
4
|
$primary = \&{"$name"}; # current definition |
|
3
|
|
|
|
|
10
|
|
30
|
3
|
|
|
|
|
13
|
$self->{primary} = \$primary; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Pre and Post hooks |
35
|
3
|
|
|
|
|
4
|
my $pre = shift; |
36
|
3
|
|
|
|
|
4
|
my $post = shift; |
37
|
3
|
|
|
|
|
5
|
my $derived = ''; |
38
|
|
|
|
|
|
|
|
39
|
3
|
|
|
|
|
5
|
$self->{callfrom} = \$callfrom; |
40
|
3
|
|
|
|
|
6
|
$self->{name} = \$name; |
41
|
3
|
|
|
|
|
4
|
$self->{pre} = \$pre; |
42
|
3
|
|
|
|
|
13
|
$self->{post} = \$post; |
43
|
3
|
|
|
|
|
6
|
$self->{derived} = \$derived; |
44
|
|
|
|
|
|
|
|
45
|
3
|
100
|
66
|
|
|
17
|
if ($pre and $post) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
46
|
|
|
|
|
|
|
} elsif ($pre) { |
47
|
0
|
|
|
0
|
|
0
|
$post = sub { @_ }; |
|
0
|
|
|
|
|
0
|
|
48
|
|
|
|
|
|
|
} elsif ($post) { |
49
|
1
|
|
|
2
|
|
4
|
$pre = sub{ @_ }; |
|
2
|
|
|
|
|
6
|
|
50
|
|
|
|
|
|
|
} else { |
51
|
1
|
|
|
3
|
|
7
|
$pre = $post = sub{ @_ }; |
|
3
|
|
|
|
|
22
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
3
|
|
|
|
|
6
|
my @result = (); |
55
|
|
|
|
|
|
|
my $sub = $derived = |
56
|
|
|
|
|
|
|
sub { |
57
|
6
|
|
|
6
|
|
32
|
@result = &$pre; |
58
|
6
|
50
|
|
|
|
55
|
if (wantarray) { # preserve the calling context |
59
|
6
|
|
|
|
|
16
|
&$post(&$primary(@result)); |
60
|
|
|
|
|
|
|
} else { |
61
|
0
|
|
|
|
|
0
|
&$post(scalar &$primary(@result)); |
62
|
|
|
|
|
|
|
} |
63
|
3
|
|
|
|
|
12
|
}; |
64
|
|
|
|
|
|
|
|
65
|
3
|
|
|
|
|
8
|
$self->install($name, $sub); |
66
|
3
|
|
|
|
|
10
|
$self; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
sub restore { |
69
|
1
|
|
|
1
|
0
|
15
|
my $self = shift; |
70
|
1
|
|
|
|
|
2
|
my $name = ${$self->{name}}; |
|
1
|
|
|
|
|
3
|
|
71
|
1
|
|
|
|
|
2
|
my $sub = ${$self->{primary}}; |
|
1
|
|
|
|
|
3
|
|
72
|
1
|
|
|
|
|
4
|
$self->install($name, $sub); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
sub install { |
75
|
4
|
|
|
4
|
0
|
7
|
my $self = shift; |
76
|
4
|
|
|
|
|
5
|
my $name = shift; |
77
|
4
|
|
|
|
|
6
|
my $sub = shift; |
78
|
1
|
|
|
1
|
|
5
|
no strict qw(refs); # some hackery and surgery |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
79
|
4
|
|
|
|
|
8
|
my $CW = $^W; $^W = 0; |
|
4
|
|
|
|
|
6
|
|
80
|
4
|
|
|
|
|
5
|
*{$name} = $sub; |
|
4
|
|
|
|
|
13
|
|
81
|
4
|
|
|
|
|
6
|
$^W = $CW; |
82
|
1
|
|
|
1
|
|
4
|
use strict qw(refs); # end of operation |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
207
|
|
83
|
4
|
|
|
|
|
8
|
$self; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
sub pre { |
86
|
1
|
|
|
1
|
0
|
10
|
my $self = shift; |
87
|
1
|
50
|
|
|
|
5
|
if (@_) { |
88
|
1
|
|
|
|
|
2
|
${$self->{pre}} = shift; |
|
1
|
|
|
|
|
4
|
|
89
|
|
|
|
|
|
|
} else { |
90
|
0
|
|
|
|
|
0
|
${$self->{pre}}; |
|
0
|
|
|
|
|
0
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
sub post { |
94
|
1
|
|
|
1
|
0
|
9
|
my $self = shift; |
95
|
1
|
50
|
|
|
|
5
|
if (@_) { |
96
|
1
|
|
|
|
|
2
|
${$self->{post}} = shift; |
|
1
|
|
|
|
|
22
|
|
97
|
|
|
|
|
|
|
} else { |
98
|
0
|
|
|
|
|
|
${$self->{post}}; |
|
0
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
sub primary { |
102
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
103
|
0
|
0
|
|
|
|
|
if (@_) { |
104
|
0
|
|
|
|
|
|
${$self->{primary}} = shift; |
|
0
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} else { |
106
|
0
|
|
|
|
|
|
${$self->{primary}}; |
|
0
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
sub derived { |
110
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
111
|
0
|
0
|
|
|
|
|
if (@_) { |
112
|
0
|
|
|
|
|
|
${$self->{derived}} = shift; |
|
0
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
} else { |
114
|
0
|
|
|
|
|
|
${$self->{derived}}; |
|
0
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
__END__ |