blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Util.pm untested

filter: Statementstmt 0.0% 0 / 158 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 303 CC 27 SCAR 28.9
CC
27
Cov
0%
apply_recursively
72.0
merge_hash
30.0
load_class
12.0
CRAP 17.9
1 package Dwarf::Util;
2 use Dwarf::Pragma;
3 use parent qw(Exporter);
4 use Encode ();
5 use File::Basename ();
6 use File::Path ();
7 use JSON ();
8 use Scalar::Util qw(blessed refaddr);
9  
10 our @EXPORT_OK = qw/
11 add_method
12 load_class
13 installed
14 capitalize
15 shuffle_array
16 filename
17 read_file
18 write_file
19 get_suffix
20 safe_join
21 merge_hash
22 random_string
23 safe_decode_json
24 encode_utf8
25 decode_utf8
26 encode_utf8_recursively
27 decode_utf8_recursively
28 apply_recursively
29 dwarf_log
30 /;
31  
32 # メソッドの追加
33 sub add_method {
34 my ($klass, $method, $code) = @_;
35 $klass = ref $klass || $klass;
36 no strict 'refs';
37 no warnings 'redefine';
38 *{"${klass}::${method}"} = $code;
39 }
40  
41 # クラスの読み込み
42 sub load_class {
43 my($class, $prefix) = @_;
44  
45 if ($prefix) {
46 unless ($class =~ s/^\+// || $class =~ /^$prefix/) {
47 $class = "$prefix\::$class";
48 }
49 }
50  
51 my $file = $class;
52 $file =~ s!::!/!g;
53 require "$file.pm";
54  
55 return $class;
56 }
57  
58 # モジュールがインストールされているかを確認
59 sub installed {
60 my ($class, $prefix) = @_;
61 my $installed = 1;
62 eval { load_class($class, $prefix) };
63 if ($@) {
64 # warn $@;
65 $@ = undef;
66 $installed = 0;
67 }
68 return $installed;
69 }
70  
71 # キャピタライズ
72 sub capitalize {
73 my $value = shift;
74 $value =~ s/-/_/g;
75 my @flagments = split '_', $value;
76 return join '', map { ucfirst $_ } @flagments;
77 }
78  
79 # 配列をシャッフル
80 sub shuffle_array {
81 my @a = @_;
82 return @a if @a == 0;
83  
84 for (my $i = @a - 1; $i >= 0; $i--) {
85 my $j = int(rand($i + 1));
86 next if $i == $j;
87 @a[$i, $j] = @a[$j, $i];
88 }
89  
90 return (@a);
91 }
92  
93 # ある Perl モジュールのファイル名を返す
94 sub filename {
95 my $invocant = shift;
96 my $class = ref $invocant || $invocant;
97 $class =~ s/::/\//g;
98 $class .= '.pm';
99 return exists $INC{$class} ? $INC{$class} : $class;
100 }
101  
102 # ファイルを読み込む
103 sub read_file {
104 my ($path, $glue) = @_;
105 $glue //= "";
106 my @body;
107 open my $fh, '<', $path or die "Couldn't open $path";
108 binmode $fh;
109 while (my $line = <$fh>) {
110 push @body, $line;
111 }
112 close $fh;
113 return join $glue, @body;
114 }
115  
116 # あるパスにコンテンツを書き出す(自動的に mkpath してくれる)
117 sub write_file {
118 my ($path, $content) = @_;
119  
120 my $dir = File::Basename::dirname($path);
121  
122 unless (-d $dir) {
123 File::Path::mkpath $dir or die "Couldn't make $dir"
124 }
125  
126 open my $fh, '>', $path or die "Couldn't open $path";
127 print $fh $content;
128 close $fh;
129 }
130  
131 # ファイルの拡張子を取得
132 sub get_suffix {
133 my $filename = shift;
134 my $suffix;
135 if ($filename =~ /.+\.(\S+?)$/) {
136 $suffix = lc $1;
137 }
138 return $suffix;
139 }
140  
141 # undef が含まれるかも知れない変数の join
142 sub safe_join {
143 my $a = shift;
144 my @b = map { defined $_ ? $_ : '' } @_;
145 join $a, @b;
146 }
147  
148 # 二つのハッシュリファレンスを簡易マージ
149 sub merge_hash {
150 my ($a, $b) = @_;
151 return $b unless defined $a;
152 return {} if ref $a ne 'HASH' or ref $b ne 'HASH';
153  
154 for my $k (%{ $b }) {
155 next unless defined $k;
156 if (defined $b->{ $k }) {
157 $a->{ $k } = $b->{ $k };
158 }
159 }
160  
161 return $a;
162 }
163  
164 # ランダム文字列
165 sub random_string {
166 my $length = shift;
167 $length ||= 32;
168 my $str = "";
169 for (1 .. $length) {
170 $str .= (0 .. 9, 'a' .. 'z')[int rand 36];
171 }
172 return $str;
173 }
174  
175 # decode_json の undef 対策
176 sub safe_decode_json {
177 my ($data) = @_;
178 return undef unless defined $data;
179 return JSON::decode_json($data);
180 }
181  
182 # Encode-2.12 以下対策
183 sub encode_utf8 {
184 my $utf8 = shift;
185 return unless defined $utf8;
186 my $bytes = Encode::is_utf8($utf8) ? Encode::encode_utf8($utf8) : $utf8;
187 return $bytes;
188 }
189  
190 # Encode-2.12 以下対策
191 sub decode_utf8 {
192 my $bytes = shift;
193 return unless defined $bytes;
194 my $utf8 = Encode::is_utf8($bytes) ? $bytes : Encode::decode_utf8($bytes);
195 return $utf8;
196 }
197  
198 # 再帰的に encode_utf8
199 sub encode_utf8_recursively {
200 my ($stuff, $check) = @_;
201 apply_recursively(sub { Encode::encode_utf8($_[0]) }, {}, $stuff);
202 }
203  
204 # 再帰的に decode_utf8
205 sub decode_utf8_recursively {
206 my ($stuff, $check) = @_;
207 apply_recursively(sub { Encode::decode_utf8($_[0], $check) }, {}, $stuff);
208 }
209  
210 # 関数の再帰
211 sub apply_recursively {
212 my $code = shift;
213 my $seen = shift;
214  
215 my @retval;
216 for my $arg (@_) {
217 if(my $ref = ref $arg){
218 my $refaddr = refaddr($arg);
219 my $proto;
220  
221 if(defined($proto = $seen->{$refaddr})){
222 # noop
223 }
224 elsif($ref eq 'ARRAY'){
225 $proto = $seen->{$refaddr} = [];
226 @{$proto} = apply_recursively($code, $seen, @{$arg});
227 }
228 elsif($ref eq 'HASH'){
229 $proto = $seen->{$refaddr} = {};
230 %{$proto} = apply_recursively($code, $seen, %{$arg});
231 }
232 elsif($ref eq 'REF' or $ref eq 'SCALAR'){
233 $proto = $seen->{$refaddr} = \do{ my $scalar };
234 ${$proto} = apply_recursively($code, $seen, ${$arg});
235 }
236 else{ # CODE, GLOB, IO, LVALUE etc.
237 $proto = $seen->{$refaddr} = $arg;
238 }
239  
240 push @retval, $proto;
241 }
242 else{
243 push @retval, defined($arg) ? $code->($arg) : $arg;
244 }
245 }
246  
247 return wantarray ? @retval : $retval[0];
248 }
249  
250 # Dwarf 開発用ロガー
251 sub dwarf_log {
252 warn @_ if defined $ENV{DWARF_LOG_LEVEL} and $ENV{DWARF_LOG_LEVEL} > 0;
253 }
254  
255 1;