line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- mode: perl; coding: utf-8 -*- |
2
|
|
|
|
|
|
|
package YATT::Util::Symbol; |
3
|
13
|
|
|
13
|
|
2823
|
use base qw(Exporter); |
|
13
|
|
|
|
|
14
|
|
|
13
|
|
|
|
|
728
|
|
4
|
13
|
|
|
13
|
|
45
|
use strict; |
|
13
|
|
|
|
|
12
|
|
|
13
|
|
|
|
|
365
|
|
5
|
13
|
|
|
13
|
|
45
|
use warnings FATAL => qw(all); |
|
13
|
|
|
|
|
15
|
|
|
13
|
|
|
|
|
747
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
BEGIN { |
8
|
13
|
|
|
13
|
|
34
|
our @EXPORT_OK = qw(class globref stash |
9
|
|
|
|
|
|
|
fields_hash fields_hash_of_class |
10
|
|
|
|
|
|
|
add_isa lift_isa_to |
11
|
|
|
|
|
|
|
declare_alias |
12
|
|
|
|
|
|
|
define_const |
13
|
|
|
|
|
|
|
rebless_with |
14
|
|
|
|
|
|
|
); |
15
|
13
|
|
|
|
|
176
|
our @EXPORT = @EXPORT_OK; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
13
|
|
|
13
|
|
44
|
use Carp; |
|
13
|
|
|
|
|
12
|
|
|
13
|
|
|
|
|
579
|
|
19
|
13
|
|
|
13
|
|
857
|
use YATT::Util qw(numeric lsearch); |
|
13
|
|
|
|
|
11
|
|
|
13
|
|
|
|
|
1038
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub class { |
22
|
18311
|
100
|
|
18311
|
0
|
91865
|
ref $_[0] || $_[0] |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub globref { |
26
|
18311
|
|
|
18311
|
0
|
20693
|
my ($thing, @name) = @_; |
27
|
13
|
|
|
13
|
|
46
|
no strict 'refs'; |
|
13
|
|
|
|
|
13
|
|
|
13
|
|
|
|
|
5396
|
|
28
|
18311
|
|
|
|
|
12746
|
\*{join("::", class($thing), @name)}; |
|
18311
|
|
|
|
|
18104
|
|
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub stash { |
32
|
0
|
|
|
0
|
0
|
0
|
*{globref($_[0], '')}{HASH} |
|
0
|
|
|
|
|
0
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub declare_alias ($$) { |
36
|
15
|
|
|
15
|
0
|
27
|
my ($name, $sub, $pack) = @_; |
37
|
15
|
|
33
|
|
|
46
|
$pack ||= caller; |
38
|
15
|
|
|
|
|
8
|
*{globref($pack, $name)} = $sub; |
|
15
|
|
|
|
|
17
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub define_const { |
42
|
199
|
|
|
199
|
0
|
168
|
my ($name_or_glob, $value) = @_; |
43
|
199
|
100
|
|
|
|
291
|
my $glob = ref $name_or_glob ? $name_or_glob : globref($name_or_glob); |
44
|
199
|
|
|
0
|
|
1106
|
*$glob = sub () { $value }; |
|
0
|
|
|
|
|
0
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub fields_hash_of_class { |
48
|
17059
|
|
|
17059
|
0
|
12015
|
*{globref($_[0], 'FIELDS')}{HASH}; |
|
17059
|
|
|
|
|
21217
|
|
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
*fields_hash = do { |
52
|
|
|
|
|
|
|
if ($] >= 5.009) { |
53
|
|
|
|
|
|
|
\&fields_hash_of_class; |
54
|
|
|
|
|
|
|
} else { |
55
|
|
|
|
|
|
|
sub { $_[0]->[0] } |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub rebless_array_with { |
60
|
0
|
|
|
0
|
0
|
0
|
my ($self, $newclass) = @_; |
61
|
0
|
|
|
|
|
0
|
$self->[0] = fields_hash_of_class($newclass); |
62
|
0
|
|
|
|
|
0
|
bless $self, $newclass; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
*rebless_with = do { |
66
|
|
|
|
|
|
|
if ($] >= 5.009) { |
67
|
|
|
|
|
|
|
require YATT::Util::SymbolHash; |
68
|
|
|
|
|
|
|
\&YATT::Util::SymbolHash::rebless_hash_with; |
69
|
|
|
|
|
|
|
} else { |
70
|
|
|
|
|
|
|
\&rebless_array_with; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub add_isa { |
75
|
169
|
|
|
169
|
0
|
295
|
my ($pack, $targetClass, @baseClass) = @_; |
76
|
169
|
|
|
|
|
381
|
my $isa = globref($targetClass, 'ISA'); |
77
|
169
|
|
|
|
|
226
|
my @uniqBase; |
78
|
169
|
50
|
|
|
|
189
|
if (my $array = *{$isa}{ARRAY}) { |
|
169
|
|
|
|
|
459
|
|
79
|
169
|
|
|
|
|
323
|
foreach my $baseClass (@baseClass) { |
80
|
169
|
100
|
|
|
|
383
|
next if $targetClass eq $baseClass; |
81
|
167
|
50
|
|
6
|
|
873
|
next if lsearch {$_ eq $baseClass} $array; |
|
6
|
|
|
|
|
23
|
|
82
|
167
|
|
|
|
|
684
|
push @uniqBase, $baseClass; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} else { |
85
|
0
|
|
|
|
|
0
|
*{$isa} = []; |
|
0
|
|
|
|
|
0
|
|
86
|
0
|
|
|
|
|
0
|
@uniqBase = @baseClass; |
87
|
|
|
|
|
|
|
} |
88
|
169
|
|
|
|
|
208
|
push @{*{$isa}{ARRAY}}, @uniqBase; |
|
169
|
|
|
|
|
151
|
|
|
169
|
|
|
|
|
1884
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub lift_isa_to { |
92
|
12
|
|
|
12
|
0
|
20
|
my ($new_parent, $child) = @_; |
93
|
12
|
|
|
|
|
16
|
my $orig = *{globref($child, 'ISA')}; |
|
12
|
|
|
|
|
27
|
|
94
|
12
|
|
|
|
|
18
|
my $isa = *{$orig}{ARRAY}; |
|
12
|
|
|
|
|
33
|
|
95
|
12
|
50
|
|
|
|
31
|
*{$orig} = $isa = [] unless $isa; |
|
0
|
|
|
|
|
0
|
|
96
|
12
|
|
|
|
|
27
|
my @orig = @$isa; |
97
|
|
|
|
|
|
|
# croak "Multiple inheritance is not supported: $child isa @orig" |
98
|
|
|
|
|
|
|
# if @orig > 1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# !!: *{$orig} = [$new_parent]; is not ok. |
101
|
12
|
|
|
|
|
114
|
@$isa = $new_parent; |
102
|
|
|
|
|
|
|
|
103
|
12
|
100
|
|
|
|
215
|
return unless @orig; |
104
|
3
|
|
|
|
|
7
|
add_isa(undef, $new_parent, @orig); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |