line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Util::Utl; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
4
|
|
|
4
|
|
331262
|
$Util::Utl::VERSION = '0.0011'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: Scalar::Util, List::Util, List::MoreUtils, String::Util & more (via one subroutine) |
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
37
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
161
|
|
8
|
4
|
|
|
4
|
|
25
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
103
|
|
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
3487
|
use Package::Pkg; |
|
4
|
|
|
|
|
141531
|
|
|
4
|
|
|
|
|
22
|
|
11
|
4
|
|
|
4
|
|
512
|
use Carp qw/ croak confess /; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
2084
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub import { |
14
|
4
|
|
|
4
|
|
50
|
my $package = caller; |
15
|
4
|
|
|
112
|
|
94
|
pkg->install( code => sub { __PACKAGE__ }, into => $package, as => 'utl' ); |
|
112
|
|
|
|
|
26921
|
|
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
6
|
|
100
|
6
|
1
|
47
|
sub empty { return not ( defined $_[1] and length $_[1] ) } |
19
|
5
|
|
100
|
5
|
1
|
54
|
sub blank { return not ( defined $_[1] and $_[1] =~ m/\S/ ) } |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub first { |
22
|
11
|
|
|
11
|
1
|
13
|
my $self = shift; |
23
|
11
|
100
|
|
|
|
38
|
goto &List::Util::first if ref $_[0] eq 'CODE'; |
24
|
10
|
|
|
|
|
18
|
unshift @_, $self; |
25
|
10
|
50
|
|
|
|
39
|
goto &_first_hash if ref $_[1] eq 'HASH'; |
26
|
0
|
|
|
|
|
0
|
confess "Invalid invocation: first (@_)"; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _first_hash { |
30
|
10
|
|
|
10
|
|
11
|
my $self = shift; |
31
|
10
|
|
|
|
|
12
|
my $hash = shift; |
32
|
10
|
|
|
|
|
19
|
my @query = @_; |
33
|
10
|
|
|
|
|
13
|
my $options = {}; |
34
|
10
|
100
|
|
|
|
24
|
$options = pop @query if ref $query[-1] eq 'HASH'; |
35
|
|
|
|
|
|
|
|
36
|
10
|
|
|
|
|
18
|
my $test = $options->{ test }; |
37
|
10
|
|
|
|
|
13
|
my $exclusive = $options->{ exclusive }; |
38
|
|
|
|
|
|
|
|
39
|
10
|
|
|
|
|
9
|
my @found; |
40
|
10
|
|
|
|
|
14
|
for my $key ( @query ) { |
41
|
25
|
100
|
|
|
|
54
|
if ( exists $hash->{ $key } ) { |
42
|
14
|
100
|
|
|
|
25
|
if ( $test ) { |
43
|
5
|
|
|
|
|
7
|
local $_ = $hash->{ $key }; |
44
|
5
|
100
|
|
|
|
11
|
next if not $test->( $_, $key, $hash ); |
45
|
|
|
|
|
|
|
} |
46
|
11
|
|
|
|
|
17
|
push @found, $key; |
47
|
11
|
100
|
|
|
|
21
|
last if not $exclusive; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
10
|
100
|
100
|
|
|
38
|
if ( $exclusive && @found > 1 ) { |
52
|
2
|
50
|
|
|
|
5
|
if ( ref $exclusive eq 'CODE' ) { |
53
|
0
|
|
|
|
|
0
|
return $exclusive->( $hash, @found ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
2
|
|
|
|
|
34
|
croak "first: Found non-exclusive keys (@found) in hash\n"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
8
|
100
|
66
|
|
|
25
|
return if $options->{ empty } && !@found; |
61
|
|
|
|
|
|
|
|
62
|
7
|
100
|
|
|
|
24
|
return undef if !@found; |
63
|
|
|
|
|
|
|
|
64
|
5
|
|
|
|
|
28
|
return $hash->{ $found[0] }; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
my $install = sub { |
69
|
|
|
|
|
|
|
my $package = shift; |
70
|
|
|
|
|
|
|
eval "require $package;" or die $@; |
71
|
|
|
|
|
|
|
my @export = @_; |
72
|
|
|
|
|
|
|
@export = eval "\@${package}::EXPORT_OK" if not @export; |
73
|
|
|
|
|
|
|
for my $method ( @export ) { |
74
|
|
|
|
|
|
|
next if __PACKAGE__->can( $method ); |
75
|
4
|
|
|
4
|
|
23
|
no strict 'refs'; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
435
|
|
76
|
0
|
|
|
0
|
|
|
*$method = eval qq/sub { shift; goto &${package}::$method };/; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|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
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
$install->( 'List::Util' ); |
81
|
|
|
|
|
|
|
$install->( 'List::MoreUtils' ); |
82
|
|
|
|
|
|
|
$install->( 'Scalar::Util' ); |
83
|
|
|
|
|
|
|
$install->( 'String::Util' ); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=pod |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 NAME |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Util::Utl - Scalar::Util, List::Util, List::MoreUtils, String::Util & more (via one subroutine) |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 VERSION |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
version 0.0011 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 SYNOPSIS |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
use Util::Utl; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
utl->first( { ... }, ... ) |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
if ( utl->blessed( ... ) ) { |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
if ( utl->looks_like_number( ... ) ) { |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 DESCRIPTION |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Util::Utl exports a single subroutine C which provides access to: |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
L |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
L |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
L |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
L |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 USAGE |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Util::Utl also provides some additional functionality |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Each function here is accessed in the same way: |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
utl->$name( ... ) |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 empty( $value ) |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Returns true if $value is undefined or has 0-length |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 blank( $value ) |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Returns true if $value is undefined or is composed only of whitespace (\s) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 first( $code, ... ) |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L::first |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 first( $hash, @query, $options ) |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
%hash = ( a => 1, b => 2, c => 3 ) |
147
|
|
|
|
|
|
|
... = utl->first( \%hash, qw/ z a b / ) # Returns 1 |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
For each name in C<@query>, test C<$hash> to see if it exists. Returns the value of |
150
|
|
|
|
|
|
|
the first entry found |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Returns undef if none exist |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
$options (a HASH reference) are: |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
exclusive True to throw an exception if more than 1 of query is present |
157
|
|
|
|
|
|
|
in $hash |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
%hash = ( a => 1, b => 2, c => 3 ) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
... = utl->first( \%hash, qw/ a b /, { exclusive => 1 } ) |
162
|
|
|
|
|
|
|
# Throws an exception (die) |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
... = utl->first( \%hash, qw/ a z /, { exclusive => 1 } ) |
165
|
|
|
|
|
|
|
# Does not throw an exception |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
test A subroutine for testing whether a value should be included or not. Can be |
168
|
|
|
|
|
|
|
used to skip over undefined or empty values |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
%hash = ( a => undef, b => '', c => 1 ) |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
... = utl->first( \%hash, qw/ a b c /, { test => sub { defined } } ) |
173
|
|
|
|
|
|
|
# Returns '' |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
empty True to return an empty list instead of undef if none are found |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
%hash = ( a => 1 ) |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
... = utl->first( \%hash, qw/ z x y / ) |
180
|
|
|
|
|
|
|
# Returns undef |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
... = utl->first( \%hash, qw/ z x y /, { empty => 1 } ) |
183
|
|
|
|
|
|
|
# Returns () |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 AUTHOR |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Robert Krimen |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
This software is copyright (c) 2011 by Robert Krimen. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
194
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
__END__ |