line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojar::Util; |
2
|
3
|
|
|
3
|
|
41929
|
use Mojo::Base -strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
20
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = 0.361; |
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
323
|
use B; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
117
|
|
7
|
3
|
|
|
3
|
|
10
|
use Carp 'croak'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
117
|
|
8
|
3
|
|
|
3
|
|
10
|
use Exporter 'import'; |
|
3
|
|
|
|
|
2
|
|
|
3
|
|
|
|
|
64
|
|
9
|
3
|
|
|
3
|
|
1206
|
use Mojo::File; |
|
3
|
|
|
|
|
257472
|
|
|
3
|
|
|
|
|
122
|
|
10
|
3
|
|
|
3
|
|
24
|
use Scalar::Util 'reftype'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
105
|
|
11
|
3
|
|
|
3
|
|
1881
|
use Storable 'dclone'; |
|
3
|
|
|
|
|
6963
|
|
|
3
|
|
|
|
|
449
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw(as_bool been_numeric check_exists dumper hash_or_hashref |
14
|
|
|
|
|
|
|
loaded_path lc_keys merge slurp_chomped snakecase spurt transcribe |
15
|
|
|
|
|
|
|
unsnakecase); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Public functions |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub as_bool { |
20
|
13
|
|
|
13
|
1
|
516
|
my ($val) = shift; |
21
|
13
|
100
|
100
|
|
|
19
|
return !! $val if been_numeric($val) or not defined $val; |
22
|
11
|
|
|
|
|
18
|
$val = lc "$val"; |
23
|
11
|
100
|
100
|
|
|
77
|
return !! 1 |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
24
|
|
|
|
|
|
|
if $val eq '1' or $val eq 'true' or $val eq 'yes' or $val eq 'on'; |
25
|
|
|
|
|
|
|
return !! undef |
26
|
7
|
100
|
100
|
|
|
43
|
if $val eq '0' or $val eq 'false' or $val eq 'no' or $val eq 'off'; |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
27
|
3
|
|
|
|
|
10
|
return !! $val; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub dumper { |
31
|
3
|
|
|
3
|
|
14
|
no warnings 'once'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
5532
|
|
32
|
11
|
|
|
11
|
1
|
935
|
require Data::Dumper; |
33
|
11
|
|
|
|
|
14
|
local $Data::Dumper::Terse = 1; |
34
|
11
|
|
|
|
|
10
|
local $Data::Dumper::Indent = 1; |
35
|
11
|
|
|
|
|
9
|
local $Data::Dumper::Quotekeys = 0; |
36
|
11
|
|
|
|
|
10
|
local $Data::Dumper::Sortkeys = 1; |
37
|
11
|
|
|
|
|
28
|
my $dump = Data::Dumper::Dumper(@_); |
38
|
11
|
|
|
|
|
504
|
$dump =~ s/\n\z//; |
39
|
11
|
|
|
|
|
43
|
return $dump; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub lc_keys { |
43
|
0
|
|
|
0
|
0
|
0
|
my ($hr) = @_; |
44
|
0
|
0
|
|
|
|
0
|
croak q{Missing required hashref} unless reftype $hr eq 'HASH'; |
45
|
0
|
|
|
|
|
0
|
%$hr = map +(lc $_ => $$hr{$_}), keys %$hr; |
46
|
0
|
|
|
|
|
0
|
return $hr; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
0
|
0
|
0
|
sub slurp_chomped { my $t = Mojo::File->new(shift)->slurp; ()while chomp $t; $t} |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub snakecase { |
52
|
20
|
|
|
20
|
1
|
1332
|
my ($string, $syllable_sep) = @_; |
53
|
20
|
|
100
|
|
|
43
|
$syllable_sep //= '_'; |
54
|
20
|
50
|
|
|
|
26
|
return undef unless defined $string; |
55
|
|
|
|
|
|
|
|
56
|
20
|
|
|
|
|
13
|
my @words; |
57
|
|
|
|
|
|
|
# Absorb any leading lowercase chars |
58
|
20
|
100
|
|
|
|
47
|
push @words, $1 if $string =~ s/^([^A-Z]+)//; |
59
|
|
|
|
|
|
|
# Absorb each titlecase substring |
60
|
20
|
|
|
|
|
111
|
push @words, lcfirst $1 while $string =~ s/\A([A-Z][^A-Z]*)//; |
61
|
20
|
|
|
|
|
29
|
for (0 .. $#words - 1) { |
62
|
15
|
100
|
|
|
|
39
|
$words[$_] .= $syllable_sep unless $words[$_] =~ /[^a-z]$/; |
63
|
|
|
|
|
|
|
} |
64
|
20
|
|
|
|
|
76
|
return join '', @words; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub unsnakecase { |
68
|
27
|
|
|
27
|
1
|
2204
|
my ($string, $separator, $want_camelcase) = @_; |
69
|
27
|
|
100
|
|
|
72
|
$separator //= '_'; |
70
|
27
|
50
|
|
|
|
34
|
return undef unless defined $string; |
71
|
|
|
|
|
|
|
|
72
|
27
|
|
|
|
|
19
|
my @words; |
73
|
|
|
|
|
|
|
# Absorb any leading separators |
74
|
27
|
100
|
|
|
|
133
|
push @words, $1 if $string =~ s/\A(\Q$separator\E+)//; |
75
|
|
|
|
|
|
|
# Absorb any leading component if doing camelcase |
76
|
27
|
100
|
66
|
|
|
62
|
if ($want_camelcase |
77
|
|
|
|
|
|
|
and $string =~ s/\A([^\Q$separator\E]+)\Q$separator\E?//) { |
78
|
2
|
|
|
|
|
4
|
push @words, $1; |
79
|
2
|
50
|
|
|
|
14
|
push @words, $1 if $string =~ s/\A(\Q$separator\E+)//; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
# Absorb each substring as titlecase |
82
|
27
|
|
|
|
|
160
|
while ($string =~ s/\A([^\Q$separator\E]+)\Q$separator\E?//) { |
83
|
37
|
|
|
|
|
63
|
push @words, ucfirst lc $1; |
84
|
37
|
100
|
|
|
|
178
|
push @words, $1 if $string =~ s/\A(\Q$separator\E+)//; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
# Fix any trailing separators |
87
|
27
|
100
|
100
|
|
|
111
|
$words[-1] .= $separator if @words && $words[-1] =~ /\A\Q$separator\E/; |
88
|
27
|
|
|
|
|
109
|
return join '', @words; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub transcribe { |
92
|
14
|
|
|
14
|
1
|
812
|
my $string = shift; |
93
|
14
|
100
|
|
|
|
30
|
my $translator = ref $_[-1] eq 'CODE' ? pop : undef; |
94
|
14
|
100
|
|
|
|
24
|
return undef unless defined $string; |
95
|
|
|
|
|
|
|
|
96
|
13
|
|
|
|
|
14
|
my $parts = [ $string ]; # arrayref tree with strings at leaves |
97
|
13
|
|
|
|
|
14
|
my @joiners = (); # joining string for each level |
98
|
13
|
|
|
|
|
14
|
my @level_parts = ( $parts ); # array of arrayrefs, each containing a string |
99
|
13
|
|
|
|
|
12
|
my @next_level_parts = (); # array of arrayrefs, each containing a string |
100
|
13
|
|
|
|
|
11
|
my ($old, $new); |
101
|
13
|
|
66
|
|
|
50
|
while (($old, $new) = (shift, shift) and defined $new) { |
102
|
20
|
|
|
|
|
19
|
push @joiners, $new; |
103
|
20
|
|
|
|
|
19
|
foreach my $p (@level_parts) { |
104
|
|
|
|
|
|
|
# $p is arrayref containing a string |
105
|
35
|
|
|
|
|
172
|
my @components = split /\Q$old/, $p->[0], -1; |
106
|
|
|
|
|
|
|
# Modify $parts tree |
107
|
35
|
|
50
|
|
|
140
|
@$p = map [ $_ // '' ], @components; |
108
|
|
|
|
|
|
|
# $p is arrayref containing arrayrefs, each containing a string |
109
|
|
|
|
|
|
|
# Set up next level |
110
|
35
|
|
|
|
|
48
|
push @next_level_parts, @$p; |
111
|
|
|
|
|
|
|
} |
112
|
20
|
|
|
|
|
22
|
@level_parts = @next_level_parts; |
113
|
20
|
|
|
|
|
66
|
@next_level_parts = (); |
114
|
|
|
|
|
|
|
} |
115
|
13
|
|
100
|
|
|
34
|
while ($translator and my $p = shift @level_parts) { |
116
|
22
|
|
|
|
|
27
|
$p->[0] = $translator->($p->[0]); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
13
|
|
|
|
|
18
|
my @traverse = ( [0, $parts] ); |
120
|
13
|
|
|
|
|
22
|
while (my $next = pop @traverse) { |
121
|
42
|
|
|
|
|
34
|
my ($depth, $ref) = @$next[0,1]; |
122
|
42
|
100
|
|
|
|
57
|
if (ref $$ref[0]) { |
123
|
41
|
100
|
|
|
|
79
|
if (my @deeper = grep ref($_->[0]), @$ref) { |
124
|
|
|
|
|
|
|
# Found some children not ready to be joined |
125
|
9
|
|
|
|
|
35
|
push @traverse, [$depth, $ref], map [$depth + 1, $_], @deeper; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
else { |
128
|
|
|
|
|
|
|
# Children all strings => join them |
129
|
32
|
|
100
|
|
|
174
|
@$ref = join $joiners[$depth], map +($_->[0] //= ''), @$ref; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
# else string => do nothing |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
13
|
|
100
|
|
|
58
|
return $parts->[0] // ''; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub loaded_path { |
139
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
140
|
|
|
|
|
|
|
# Try .pm |
141
|
0
|
|
0
|
|
|
0
|
(my $module = (ref $self // $self) .'.pm') =~ s{::}{/}; |
142
|
0
|
0
|
|
|
|
0
|
return $INC{$module} if exists $INC{$module}; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Try .pl |
145
|
0
|
|
|
|
|
0
|
$module =~ s{\.pm$}{.pl}; |
146
|
0
|
0
|
|
|
|
0
|
return $INC{$module} if exists $INC{$module}; |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
0
|
return undef; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub been_numeric { |
152
|
13
|
|
|
13
|
1
|
13
|
my $value = shift; |
153
|
|
|
|
|
|
|
# From Mojo::JSON |
154
|
13
|
50
|
66
|
|
|
164
|
return 1 if B::svref_2object(\$value)->FLAGS & (B::SVp_IOK | B::SVp_NOK) |
|
|
|
66
|
|
|
|
|
155
|
|
|
|
|
|
|
and 0 + $value eq $value and $value * 0 == 0; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub spurt (@) { |
159
|
4
|
|
|
4
|
0
|
2275
|
my $path = shift; |
160
|
4
|
100
|
|
|
|
13
|
my $lines = ref $_[-1] eq 'ARRAY' ? pop : \@_; |
161
|
4
|
|
|
|
|
4
|
my $count = 0; |
162
|
|
|
|
|
|
|
|
163
|
4
|
50
|
|
|
|
204
|
die qq{Can't open file "$path": $!} unless open my $file, '>', $path; |
164
|
4
|
|
|
|
|
21
|
$file->syswrite(''); |
165
|
4
|
|
|
|
|
38
|
local $_; |
166
|
4
|
|
33
|
|
|
11
|
$file->syswrite($_), $file->syswrite($/) and ++$count for @$lines; |
167
|
4
|
|
|
|
|
162
|
close $file; |
168
|
4
|
|
|
|
|
24
|
return $count; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
sub hash_or_hashref { |
172
|
10
|
100
|
|
10
|
1
|
869
|
return { @_ } if @_ % 2 == 0; # hash |
173
|
5
|
50
|
66
|
|
|
29
|
return $_[0] if ref $_[0] eq 'HASH' or reftype $_[0] eq 'HASH'; |
174
|
0
|
|
|
|
|
0
|
croak sprintf 'Hash not identified (%s)', join ',', @_; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub check_exists { |
178
|
6
|
|
|
6
|
1
|
2478
|
my $requireds = shift; |
179
|
6
|
|
|
|
|
13
|
my $param = hash_or_hashref(@_); |
180
|
6
|
100
|
|
|
|
16
|
$requireds = [$requireds] unless ref $requireds eq 'ARRAY'; |
181
|
|
|
|
|
|
|
|
182
|
6
|
|
66
|
|
|
280
|
exists $param->{$_} or croak "Missing required param ($_)" for @$requireds; |
183
|
4
|
|
|
|
|
21
|
return @$param{@$requireds}; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# Private function |
187
|
|
|
|
|
|
|
sub _merge ($;$) { |
188
|
0
|
|
|
0
|
|
|
my ($left, $right) = @_; |
189
|
0
|
0
|
|
|
|
|
if (reftype $left eq 'ARRAY') { |
190
|
0
|
0
|
|
|
|
|
if (reftype $right eq 'ARRAY') { |
191
|
0
|
|
|
|
|
|
%{$left->[0]} = (%{$left->[0]}, %{ dclone($right->[0]) }); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
else { |
194
|
|
|
|
|
|
|
# $right : HASHREF |
195
|
0
|
|
|
|
|
|
%{$left->[0]} = (%{$left->[0]}, %{ dclone($right) }); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
else { |
199
|
|
|
|
|
|
|
# $left : HASHREF |
200
|
0
|
0
|
|
|
|
|
if (reftype($right) eq 'ARRAY') { |
201
|
0
|
|
|
|
|
|
%$left = (%$left, %{ dclone($right->[0]) }); |
|
0
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
else { |
204
|
|
|
|
|
|
|
# $right : HASHREF |
205
|
0
|
|
|
|
|
|
%$left = (%$left, %{ dclone($right) }); |
|
0
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
} |
208
|
0
|
|
|
|
|
|
return $left; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub merge (@); |
212
|
|
|
|
|
|
|
sub merge (@) { |
213
|
|
|
|
|
|
|
# Both class & object function |
214
|
|
|
|
|
|
|
# my $class = (@_ and not ref $_[0]) ? shift : undef; |
215
|
0
|
0
|
|
0
|
0
|
|
my $class = shift unless ref $_[0]; |
216
|
|
|
|
|
|
|
# defined($class) <=> class method |
217
|
0
|
0
|
|
|
|
|
return undef unless @_; |
218
|
0
|
|
|
|
|
|
my $left = shift; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# $left is a ref; @right could be various things |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# If called as object method |
223
|
|
|
|
|
|
|
# 'owning' (ie leftmost) object gets modified |
224
|
|
|
|
|
|
|
# If called as class method |
225
|
|
|
|
|
|
|
# a new object is created for the result |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
# It is important that the merge associates to the left |
228
|
|
|
|
|
|
|
# [ie ($a merge $b) merge $c], in contrast to Hash::Util::Simple. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
# class method => new object |
231
|
|
|
|
|
|
|
# this is done at most once per original call |
232
|
0
|
0
|
|
|
|
|
if ($class) { |
233
|
0
|
0
|
|
|
|
|
if ($left->can('clone')) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
234
|
0
|
|
|
|
|
|
return merge $left->clone, @_; |
235
|
|
|
|
|
|
|
} |
236
|
|
|
|
|
|
|
elsif ($left->can('new')) { |
237
|
0
|
|
|
|
|
|
return merge $left->new, @_; |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
elsif (ref $left eq 'HASH') { |
240
|
0
|
|
|
|
|
|
$left = dclone($left); |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
else { |
243
|
0
|
|
|
|
|
|
croak "Unable to clone first argument\n". dumper $left; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
# Base case |
248
|
0
|
0
|
|
|
|
|
unless (@_) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
return $left; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
# Recurse |
252
|
0
|
0
|
|
|
|
|
elsif (@_ == 1 and ref $_[0]) { |
253
|
|
|
|
|
|
|
# object or maybe hash ref |
254
|
0
|
|
|
|
|
|
return _merge($left, $_[0]); |
255
|
|
|
|
|
|
|
} |
256
|
0
|
0
|
|
|
|
|
elsif (@_ > 1 and ref $_[0]) { |
257
|
|
|
|
|
|
|
# object or maybe hash ref |
258
|
0
|
|
|
|
|
|
my $right = shift; |
259
|
0
|
|
|
|
|
|
return merge _merge($left, $right), @_; |
260
|
|
|
|
|
|
|
} |
261
|
0
|
0
|
|
|
|
|
elsif (@_ > 1 and @_ % 2 == 0) { |
262
|
|
|
|
|
|
|
# assume plain hash |
263
|
0
|
|
|
|
|
|
return _merge($left, { @_ }); |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
else { |
266
|
0
|
|
|
|
|
|
croak 'Tried to merge incompatible/non-object'. $/ . dumper(@_); |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
1; |
271
|
|
|
|
|
|
|
__END__ |