line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- mode: perl; coding: utf-8 -*- |
2
|
|
|
|
|
|
|
package YATT::Util::Symbol; |
3
|
13
|
|
|
13
|
|
11844
|
use base qw(Exporter); |
|
13
|
|
|
|
|
22
|
|
|
13
|
|
|
|
|
974
|
|
4
|
13
|
|
|
13
|
|
76
|
use strict; |
|
13
|
|
|
|
|
23
|
|
|
13
|
|
|
|
|
325
|
|
5
|
13
|
|
|
13
|
|
61
|
use warnings qw(FATAL all NONFATAL misc); |
|
13
|
|
|
|
|
21
|
|
|
13
|
|
|
|
|
1052
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
BEGIN { |
8
|
13
|
|
|
13
|
|
49
|
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
|
|
|
|
|
287
|
our @EXPORT = @EXPORT_OK; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
13
|
|
|
13
|
|
62
|
use Carp; |
|
13
|
|
|
|
|
31
|
|
|
13
|
|
|
|
|
904
|
|
19
|
13
|
|
|
13
|
|
1251
|
use YATT::Util qw(numeric lsearch); |
|
13
|
|
|
|
|
23
|
|
|
13
|
|
|
|
|
1524
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub class { |
22
|
18311
|
100
|
|
18311
|
0
|
147321
|
ref $_[0] || $_[0] |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub globref { |
26
|
18311
|
|
|
18311
|
0
|
36826
|
my ($thing, @name) = @_; |
27
|
13
|
|
|
13
|
|
67
|
no strict 'refs'; |
|
13
|
|
|
|
|
27
|
|
|
13
|
|
|
|
|
8619
|
|
28
|
18311
|
|
|
|
|
20618
|
\*{join("::", class($thing), @name)}; |
|
18311
|
|
|
|
|
32232
|
|
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub stash { |
32
|
0
|
|
|
|
|
0
|
*{globref($_[0], '')}{HASH} |
33
|
0
|
|
|
0
|
0
|
0
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub declare_alias ($$) { |
36
|
15
|
|
|
15
|
0
|
25
|
my ($name, $sub, $pack) = @_; |
37
|
15
|
|
33
|
|
|
68
|
$pack ||= caller; |
38
|
15
|
|
|
|
|
18
|
*{globref($pack, $name)} = $sub; |
|
15
|
|
|
|
|
25
|
|
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub define_const { |
42
|
199
|
|
|
199
|
0
|
324
|
my ($name_or_glob, $value) = @_; |
43
|
199
|
100
|
|
|
|
485
|
my $glob = ref $name_or_glob ? $name_or_glob : globref($name_or_glob); |
44
|
199
|
|
|
0
|
|
1730
|
*$glob = sub () { $value }; |
|
0
|
|
|
|
|
0
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub fields_hash_of_class { |
48
|
17059
|
|
|
17059
|
0
|
19176
|
*{globref($_[0], 'FIELDS')}{HASH}; |
|
17059
|
|
|
|
|
33202
|
|
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
|
422
|
my ($pack, $targetClass, @baseClass) = @_; |
76
|
169
|
|
|
|
|
416
|
my $isa = globref($targetClass, 'ISA'); |
77
|
169
|
|
|
|
|
325
|
my @uniqBase; |
78
|
169
|
50
|
|
|
|
262
|
if (my $array = *{$isa}{ARRAY}) { |
|
169
|
|
|
|
|
509
|
|
79
|
169
|
|
|
|
|
416
|
foreach my $baseClass (@baseClass) { |
80
|
169
|
100
|
|
|
|
467
|
next if $targetClass eq $baseClass; |
81
|
167
|
50
|
|
6
|
|
1067
|
next if lsearch {$_ eq $baseClass} $array; |
|
6
|
|
|
|
|
36
|
|
82
|
167
|
|
|
|
|
792
|
push @uniqBase, $baseClass; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} else { |
85
|
0
|
|
|
|
|
0
|
*{$isa} = []; |
|
0
|
|
|
|
|
0
|
|
86
|
0
|
|
|
|
|
0
|
@uniqBase = @baseClass; |
87
|
|
|
|
|
|
|
} |
88
|
169
|
|
|
|
|
320
|
push @{*{$isa}{ARRAY}}, @uniqBase; |
|
169
|
|
|
|
|
220
|
|
|
169
|
|
|
|
|
2489
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub lift_isa_to { |
92
|
12
|
|
|
12
|
0
|
27
|
my ($new_parent, $child) = @_; |
93
|
12
|
|
|
|
|
17
|
my $orig = *{globref($child, 'ISA')}; |
|
12
|
|
|
|
|
30
|
|
94
|
12
|
|
|
|
|
27
|
my $isa = *{$orig}{ARRAY}; |
|
12
|
|
|
|
|
40
|
|
95
|
12
|
50
|
|
|
|
42
|
*{$orig} = $isa = [] unless $isa; |
|
0
|
|
|
|
|
0
|
|
96
|
12
|
|
|
|
|
33
|
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
|
|
|
|
|
156
|
@$isa = $new_parent; |
102
|
|
|
|
|
|
|
|
103
|
12
|
100
|
|
|
|
299
|
return unless @orig; |
104
|
3
|
|
|
|
|
9
|
add_isa(undef, $new_parent, @orig); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |