line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
6
|
|
|
6
|
|
329766
|
use strict; use warnings; |
|
6
|
|
|
6
|
|
60
|
|
|
6
|
|
|
|
|
140
|
|
|
6
|
|
|
|
|
25
|
|
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
294
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Class::Observable; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '2.004'; |
6
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
29
|
use Scalar::Util 'refaddr'; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
246
|
|
8
|
6
|
|
|
6
|
|
1999
|
use Class::ISA; |
|
6
|
|
|
|
|
10425
|
|
|
6
|
|
|
|
|
1308
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Unused; kept for backward compatibility only |
11
|
|
|
|
|
|
|
my ( $DEBUG ); |
12
|
0
|
|
|
0
|
0
|
0
|
sub DEBUG { return $DEBUG; } |
13
|
0
|
|
|
0
|
0
|
0
|
sub SET_DEBUG { $DEBUG = $_[0] } |
14
|
0
|
0
|
|
0
|
0
|
0
|
sub observer_log { shift; $DEBUG && warn @_, "\n" } |
|
0
|
|
|
|
|
0
|
|
15
|
0
|
|
|
0
|
0
|
0
|
sub observer_error { shift; die @_, "\n" } |
|
0
|
|
|
|
|
0
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my ( %O, %registry ); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
BEGIN { |
20
|
6
|
|
|
6
|
|
76
|
require Config; |
21
|
6
|
50
|
33
|
|
|
106
|
if ( $^O eq 'Win32' or $Config::Config{'useithreads'} ) { |
22
|
0
|
|
|
|
|
0
|
*NEEDS_REGISTRY = sub () { 1 }; |
23
|
|
|
|
|
|
|
*CLONE = sub { |
24
|
0
|
|
|
|
|
0
|
my $have_warned; |
25
|
0
|
|
|
|
|
0
|
foreach my $oldaddr ( keys %registry ) { |
26
|
0
|
|
|
|
|
0
|
my $invocant = delete $registry{ $oldaddr }; |
27
|
0
|
|
|
|
|
0
|
my $observers = delete $O{ $oldaddr }; |
28
|
0
|
0
|
|
|
|
0
|
if ( defined $invocant ) { |
29
|
0
|
|
|
|
|
0
|
my $addr = refaddr $invocant; |
30
|
0
|
|
|
|
|
0
|
$O{ $addr } = $observers; |
31
|
0
|
|
|
|
|
0
|
Scalar::Util::weaken( $registry{ $addr } = $invocant ); |
32
|
|
|
|
|
|
|
} else { |
33
|
0
|
0
|
|
|
|
0
|
$have_warned++ or warn |
34
|
|
|
|
|
|
|
"*** Inconsistent state ***\n", |
35
|
|
|
|
|
|
|
"Observed instances have gone away " . |
36
|
|
|
|
|
|
|
"without invoking Class::Observable::DESTROY\n"; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
0
|
|
|
|
|
0
|
}; |
40
|
|
|
|
|
|
|
} else { |
41
|
6
|
|
|
|
|
3351
|
*NEEDS_REGISTRY = sub () { 0 }; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub DESTROY { |
46
|
43
|
|
|
43
|
|
14057
|
my $invocant = shift; |
47
|
43
|
|
|
|
|
84
|
my $addr = refaddr $invocant; |
48
|
43
|
|
|
|
|
49
|
delete $registry{ $addr } if NEEDS_REGISTRY and $addr; |
49
|
43
|
|
33
|
|
|
593
|
delete $O{ $addr || "::$invocant" }; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub add_observer { |
53
|
50
|
|
|
50
|
0
|
755
|
my $invocant = shift; |
54
|
50
|
|
|
|
|
85
|
my $addr = refaddr $invocant; |
55
|
50
|
|
|
|
|
54
|
Scalar::Util::weaken( $registry{ $addr } = $invocant ) if NEEDS_REGISTRY and $addr; |
56
|
50
|
|
66
|
|
|
52
|
push @{ $O{ $addr || "::$invocant" } }, @_; |
|
50
|
|
|
|
|
207
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub delete_observer { |
60
|
4
|
|
|
4
|
0
|
1902
|
my $invocant = shift; |
61
|
4
|
|
|
|
|
13
|
my $addr = refaddr $invocant; |
62
|
4
|
50
|
66
|
|
|
29
|
my $observers = $O{ $addr || "::$invocant" } or return 0; |
63
|
4
|
|
66
|
|
|
30
|
my %removal = map +( refaddr( $_ ) || "::$_" => 1 ), @_; |
64
|
4
|
|
66
|
|
|
28
|
@$observers = grep !$removal{ refaddr( $_ ) || "::$_" }, @$observers; |
65
|
4
|
100
|
|
|
|
12
|
if ( ! @$observers ) { |
66
|
2
|
|
|
|
|
3
|
delete $registry{ $addr } if NEEDS_REGISTRY and $addr; |
67
|
2
|
|
66
|
|
|
9
|
delete $O{ $addr || "::$invocant" }; |
68
|
|
|
|
|
|
|
} |
69
|
4
|
|
|
|
|
16
|
scalar @$observers; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub delete_all_observers { |
73
|
6
|
|
|
6
|
0
|
12
|
my $invocant = shift; |
74
|
6
|
|
|
|
|
15
|
my $addr = refaddr $invocant; |
75
|
6
|
|
|
|
|
6
|
delete $registry{ $addr } if NEEDS_REGISTRY and $addr; |
76
|
6
|
|
66
|
|
|
25
|
my $removed = delete $O{ $addr || "::$invocant" }; |
77
|
6
|
100
|
|
|
|
26
|
$removed ? scalar @$removed : 0; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Backward compatibility |
81
|
|
|
|
|
|
|
*delete_observers = \&delete_all_observers; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub notify_observers { |
84
|
15
|
|
|
15
|
0
|
102
|
for ( $_[0]->get_observers ) { |
85
|
35
|
100
|
|
|
|
155
|
ref eq 'CODE' ? $_->( @_ ) : $_->update( @_ ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my %supers; |
90
|
|
|
|
|
|
|
sub get_observers { |
91
|
36
|
|
|
36
|
0
|
47
|
my ( @self, $class ); |
92
|
36
|
100
|
|
|
|
75
|
if ( my $pkg = ref $_[0] ) { |
93
|
28
|
|
|
|
|
46
|
@self = $_[0]; |
94
|
28
|
|
|
|
|
32
|
$class = $pkg; |
95
|
|
|
|
|
|
|
} else { |
96
|
8
|
|
|
|
|
12
|
$class = $_[0]; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# We only find the parents the first time, so if you muck with |
100
|
|
|
|
|
|
|
# @ISA you'll get unexpected behavior... |
101
|
36
|
|
100
|
|
|
92
|
my $cached_supers = $supers{ $class } ||= [ |
102
|
|
|
|
|
|
|
grep $_->isa( 'Class::Observable' ), Class::ISA::super_path( $class ) |
103
|
|
|
|
|
|
|
]; |
104
|
|
|
|
|
|
|
|
105
|
36
|
|
|
|
|
404
|
map $_->get_direct_observers, @self, $class, @$cached_supers; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub copy_observers { |
109
|
1
|
|
|
1
|
0
|
3
|
my ( $src, $dst ) = @_; |
110
|
1
|
|
|
|
|
3
|
my @observer = $src->get_observers; |
111
|
1
|
|
|
|
|
4
|
$dst->add_observer( @observer ); |
112
|
1
|
|
|
|
|
4
|
scalar @observer; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
20
|
|
|
20
|
0
|
54
|
sub count_observers { scalar $_[0]->get_observers } |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub get_direct_observers { |
118
|
105
|
|
|
105
|
0
|
118
|
my $invocant = shift; |
119
|
105
|
|
|
|
|
133
|
my $addr = refaddr $invocant; |
120
|
105
|
50
|
66
|
|
|
404
|
my $observers = $O{ $addr || "::$invocant" } or return wantarray ? () : 0; |
|
|
100
|
|
|
|
|
|
121
|
52
|
|
|
|
|
107
|
@$observers; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
__END__ |