line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Statocles::Util; |
2
|
|
|
|
|
|
|
our $VERSION = '0.086'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Various utility functions to reduce dependencies |
4
|
|
|
|
|
|
|
|
5
|
68
|
|
|
68
|
|
381
|
use Statocles::Base; |
|
68
|
|
|
|
|
147
|
|
|
68
|
|
|
|
|
385
|
|
6
|
68
|
|
|
68
|
|
2023
|
use Exporter 'import'; |
|
68
|
|
|
|
|
118
|
|
|
68
|
|
|
|
|
1761
|
|
7
|
68
|
|
|
68
|
|
17937
|
use Mojo::JSON qw( to_json ); |
|
68
|
|
|
|
|
6319699
|
|
|
68
|
|
|
|
|
20330
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
10
|
|
|
|
|
|
|
trim dircopy run_editor uniq_by derp |
11
|
|
|
|
|
|
|
); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#pod =sub trim |
14
|
|
|
|
|
|
|
#pod |
15
|
|
|
|
|
|
|
#pod my $trimmed = trim $untrimmed; |
16
|
|
|
|
|
|
|
#pod |
17
|
|
|
|
|
|
|
#pod Trim the leading and trailing whitespace from the given scalar. |
18
|
|
|
|
|
|
|
#pod |
19
|
|
|
|
|
|
|
#pod =cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub trim(_) { |
22
|
8
|
100
|
|
8
|
1
|
70815
|
return $_[0] if !$_[0]; |
23
|
6
|
|
|
|
|
31
|
$_[0] =~ s/^\s+//; |
24
|
6
|
|
|
|
|
34
|
$_[0] =~ s/\s+$//; |
25
|
6
|
|
|
|
|
25
|
return $_[0]; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#pod =sub dircopy |
29
|
|
|
|
|
|
|
#pod |
30
|
|
|
|
|
|
|
#pod dircopy $source, $destination; |
31
|
|
|
|
|
|
|
#pod |
32
|
|
|
|
|
|
|
#pod Copy everything in $source to $destination, recursively. |
33
|
|
|
|
|
|
|
#pod |
34
|
|
|
|
|
|
|
#pod =cut |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub dircopy($$) { |
37
|
32
|
|
|
32
|
1
|
313708
|
my ( $source, $destination ) = @_; |
38
|
32
|
|
|
|
|
149
|
$source = Path::Tiny->new( $source ); |
39
|
32
|
|
|
|
|
939
|
$destination = Path::Tiny->new( $destination ); |
40
|
32
|
|
|
|
|
642
|
$destination->mkpath; |
41
|
32
|
|
|
|
|
10238
|
my $iter = $source->iterator({ recurse => 1 }); |
42
|
32
|
|
|
|
|
1244
|
while ( my $thing = $iter->() ) { |
43
|
814
|
|
|
|
|
248383
|
my $relative = $thing->relative( $source ); |
44
|
814
|
100
|
|
|
|
137831
|
if ( $thing->is_dir ) { |
45
|
403
|
|
|
|
|
6341
|
mkdir $destination->child( $relative ); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
411
|
|
|
|
|
6301
|
$thing->copy( $destination->child( $relative ) ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
#pod =sub run_editor |
54
|
|
|
|
|
|
|
#pod |
55
|
|
|
|
|
|
|
#pod my $was_run = run_editor( $path ); |
56
|
|
|
|
|
|
|
#pod |
57
|
|
|
|
|
|
|
#pod Invoke the user's text editor (from the C<EDITOR> environment variable) to edit |
58
|
|
|
|
|
|
|
#pod the given path. Returns true if an editor was invoked, false otherwise. If the |
59
|
|
|
|
|
|
|
#pod editor was not able to be invoked (C<EDITOR> was set but could not be run), an |
60
|
|
|
|
|
|
|
#pod exception is thrown. |
61
|
|
|
|
|
|
|
#pod |
62
|
|
|
|
|
|
|
#pod =cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub run_editor { |
65
|
9
|
|
|
9
|
1
|
25267
|
my ( $path ) = @_; |
66
|
9
|
100
|
|
|
|
53
|
return 0 unless $ENV{EDITOR}; |
67
|
68
|
|
|
68
|
|
738
|
no warnings 'exec'; # We're checking everything ourselves |
|
68
|
|
|
|
|
144
|
|
|
68
|
|
|
|
|
25335
|
|
68
|
5
|
|
|
|
|
44568
|
system split( /\s+/, $ENV{EDITOR} ), $path; |
69
|
5
|
100
|
|
|
|
240
|
if ($? == -1) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
70
|
1
|
|
|
|
|
57
|
die sprintf qq{Failed to invoke editor "%s": %s\n}, $ENV{EDITOR}, $!; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
elsif ($? & 127) { |
73
|
1
|
|
|
|
|
60
|
die sprintf qq{Editor "%s" died from signal %d\n}, $ENV{EDITOR}, ( $? & 127 ); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
elsif ( my $exit = $? >> 8 ) { |
76
|
1
|
|
|
|
|
51
|
die sprintf qq{Editor "%s" exited with error (non-zero) status: %d\n}, $ENV{EDITOR}, $exit; |
77
|
|
|
|
|
|
|
} |
78
|
2
|
|
|
|
|
180
|
return 1; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
#pod =sub uniq_by |
82
|
|
|
|
|
|
|
#pod |
83
|
|
|
|
|
|
|
#pod my @uniq_links = uniq_by { $_->href } @links; |
84
|
|
|
|
|
|
|
#pod |
85
|
|
|
|
|
|
|
#pod Filter a list into its unique items based on the result of the passed-in block. |
86
|
|
|
|
|
|
|
#pod This lets us get unique links from their C<href> attribute. |
87
|
|
|
|
|
|
|
#pod |
88
|
|
|
|
|
|
|
#pod =cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub uniq_by(&@) { |
91
|
2161
|
|
|
2161
|
1
|
28490
|
my ( $sub, @list ) = @_; |
92
|
2161
|
|
|
|
|
3409
|
my ( %found, @out ); |
93
|
2161
|
|
|
|
|
4204
|
for my $i ( @list ) { |
94
|
1769
|
|
|
|
|
4853
|
local $_ = $i; |
95
|
1769
|
100
|
|
|
|
3810
|
push @out, $i if !$found{ $sub->() }++; |
96
|
|
|
|
|
|
|
} |
97
|
2161
|
|
|
|
|
17381
|
return @out; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
#pod =sub derp |
101
|
|
|
|
|
|
|
#pod |
102
|
|
|
|
|
|
|
#pod derp "This feature is deprecated in file '%s'", $file; |
103
|
|
|
|
|
|
|
#pod |
104
|
|
|
|
|
|
|
#pod Print out a deprecation message as a warning. A message will only be |
105
|
|
|
|
|
|
|
#pod printed once for each set of arguments. |
106
|
|
|
|
|
|
|
#pod |
107
|
|
|
|
|
|
|
#pod =cut |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
our %DERPED; |
110
|
|
|
|
|
|
|
sub derp(@) { |
111
|
23
|
|
|
23
|
1
|
25510
|
my @args = @_; |
112
|
23
|
|
|
|
|
119
|
my $key = to_json \@args; |
113
|
23
|
100
|
|
|
|
1197
|
return if $DERPED{ $key }; |
114
|
15
|
100
|
|
|
|
94
|
if ( $args[0] !~ /\.$/ ) { |
115
|
5
|
|
|
|
|
12
|
$args[0] .= '.'; |
116
|
|
|
|
|
|
|
} |
117
|
15
|
|
|
|
|
292
|
warn sprintf( $args[0], @args[1..$#args] ). " See Statocles::Help::Upgrading\n"; |
118
|
15
|
|
|
|
|
152
|
$DERPED{ $key } = 1; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
__END__ |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=pod |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=encoding UTF-8 |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 NAME |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Statocles::Util - Various utility functions to reduce dependencies |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 VERSION |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
version 0.086 |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SYNOPSIS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
use Statocles::Util qw( dircopy ); |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
dircopy $source, $destination; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 DESCRIPTION |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This module contains some utility functions to help reduce non-core dependencies. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 SUBROUTINES |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 trim |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
my $trimmed = trim $untrimmed; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Trim the leading and trailing whitespace from the given scalar. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 dircopy |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
dircopy $source, $destination; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Copy everything in $source to $destination, recursively. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 run_editor |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
my $was_run = run_editor( $path ); |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Invoke the user's text editor (from the C<EDITOR> environment variable) to edit |
166
|
|
|
|
|
|
|
the given path. Returns true if an editor was invoked, false otherwise. If the |
167
|
|
|
|
|
|
|
editor was not able to be invoked (C<EDITOR> was set but could not be run), an |
168
|
|
|
|
|
|
|
exception is thrown. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 uniq_by |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my @uniq_links = uniq_by { $_->href } @links; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Filter a list into its unique items based on the result of the passed-in block. |
175
|
|
|
|
|
|
|
This lets us get unique links from their C<href> attribute. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 derp |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
derp "This feature is deprecated in file '%s'", $file; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Print out a deprecation message as a warning. A message will only be |
182
|
|
|
|
|
|
|
printed once for each set of arguments. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 AUTHOR |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Doug Bell <preaction@cpan.org> |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
This software is copyright (c) 2016 by Doug Bell. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
193
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=cut |