line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
6
|
|
|
6
|
|
355701
|
use strict; use warnings; |
|
6
|
|
|
6
|
|
59
|
|
|
6
|
|
|
|
|
156
|
|
|
6
|
|
|
|
|
27
|
|
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
340
|
|
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Class::Observable; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '2.003'; |
6
|
|
|
|
|
|
|
|
7
|
6
|
|
|
6
|
|
35
|
use Scalar::Util 'refaddr'; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
290
|
|
8
|
6
|
|
|
6
|
|
2298
|
use Class::ISA; |
|
6
|
|
|
|
|
11645
|
|
|
6
|
|
|
|
|
1531
|
|
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
|
|
44
|
require Config; |
21
|
6
|
50
|
33
|
|
|
119
|
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
|
|
|
|
|
3857
|
*NEEDS_REGISTRY = sub () { 0 }; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub DESTROY { |
46
|
43
|
|
|
43
|
|
15967
|
my $invocant = shift; |
47
|
43
|
|
|
|
|
89
|
my $addr = refaddr $invocant; |
48
|
43
|
|
|
|
|
53
|
delete $registry{ $addr } if NEEDS_REGISTRY and $addr; |
49
|
43
|
|
33
|
|
|
655
|
delete $O{ $addr || "::$invocant" }; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub add_observer { |
53
|
50
|
|
|
50
|
0
|
801
|
my $invocant = shift; |
54
|
50
|
|
|
|
|
96
|
my $addr = refaddr $invocant; |
55
|
50
|
|
|
|
|
60
|
Scalar::Util::weaken( $registry{ $addr } = $invocant ) if NEEDS_REGISTRY and $addr; |
56
|
50
|
|
66
|
|
|
60
|
push @{ $O{ $addr || "::$invocant" } }, @_; |
|
50
|
|
|
|
|
264
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub delete_observer { |
60
|
4
|
|
|
4
|
0
|
2239
|
my $invocant = shift; |
61
|
4
|
|
|
|
|
14
|
my $addr = refaddr $invocant; |
62
|
4
|
50
|
66
|
|
|
30
|
my $observers = $O{ $addr || "::$invocant" } or return 0; |
63
|
4
|
|
66
|
|
|
33
|
my %removal = map +( refaddr( $_ ) || "::$_" => 1 ), @_; |
64
|
4
|
|
66
|
|
|
29
|
@$observers = grep !$removal{ refaddr( $_ ) || "::$_" }, @$observers; |
65
|
4
|
100
|
|
|
|
16
|
if ( ! @$observers ) { |
66
|
2
|
|
|
|
|
2
|
delete $registry{ $addr } if NEEDS_REGISTRY and $addr; |
67
|
2
|
|
66
|
|
|
11
|
delete $O{ $addr || "::$invocant" }; |
68
|
|
|
|
|
|
|
} |
69
|
4
|
|
|
|
|
17
|
scalar @$observers; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub delete_all_observers { |
73
|
6
|
|
|
6
|
0
|
12
|
my $invocant = shift; |
74
|
6
|
|
|
|
|
16
|
my $addr = refaddr $invocant; |
75
|
6
|
|
|
|
|
7
|
delete $registry{ $addr } if NEEDS_REGISTRY and $addr; |
76
|
6
|
|
66
|
|
|
26
|
my $removed = delete $O{ $addr || "::$invocant" }; |
77
|
6
|
100
|
|
|
|
27
|
$removed ? scalar @$removed : 0; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Backward compatibility |
81
|
|
|
|
|
|
|
*delete_observers = \&delete_all_observers; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub notify_observers { |
84
|
15
|
|
|
15
|
0
|
124
|
for ( $_[0]->get_observers ) { |
85
|
35
|
100
|
|
|
|
185
|
ref eq 'CODE' ? $_->( @_ ) : $_->update( @_ ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my %supers; |
90
|
|
|
|
|
|
|
sub get_observers { |
91
|
36
|
|
|
36
|
0
|
54
|
my ( @self, $class ); |
92
|
36
|
100
|
|
|
|
83
|
if ( my $pkg = ref $_[0] ) { |
93
|
28
|
|
|
|
|
57
|
@self = $_[0]; |
94
|
28
|
|
|
|
|
35
|
$class = $pkg; |
95
|
|
|
|
|
|
|
} else { |
96
|
8
|
|
|
|
|
14
|
$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
|
|
|
103
|
my $cached_supers = $supers{ $class } ||= [ |
102
|
|
|
|
|
|
|
grep $_->isa( 'Class::Observable' ), Class::ISA::super_path( $class ) |
103
|
|
|
|
|
|
|
]; |
104
|
|
|
|
|
|
|
|
105
|
36
|
|
|
|
|
467
|
map $_->get_direct_observers, @self, $class, @$cached_supers; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub copy_observers { |
109
|
1
|
|
|
1
|
0
|
4
|
my ( $src, $dst ) = @_; |
110
|
1
|
|
|
|
|
5
|
my @observer = $src->get_observers; |
111
|
1
|
|
|
|
|
3
|
$dst->add_observer( @observer ); |
112
|
1
|
|
|
|
|
4
|
scalar @observer; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
20
|
|
|
20
|
0
|
64
|
sub count_observers { scalar $_[0]->get_observers } |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub get_direct_observers { |
118
|
105
|
|
|
105
|
0
|
134
|
my $invocant = shift; |
119
|
105
|
|
|
|
|
153
|
my $addr = refaddr $invocant; |
120
|
105
|
50
|
66
|
|
|
447
|
my $observers = $O{ $addr || "::$invocant" } or return wantarray ? () : 0; |
|
|
100
|
|
|
|
|
|
121
|
52
|
|
|
|
|
124
|
@$observers; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
1; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
__END__ |