blib/lib/Util/Any.pm

filter: Statementstmt 98.2% 343 / 349 Branchbran 85.9% 165 / 192 Conditioncond 83.9% 73 / 87 MC/DCmcdc 62.9% 34 / 54 Subroutinesub 100% 40 / 40 Podpod n/a totaltotal 90.7% 655 / 722 CC 149 SCAR 28.1
CC
149
Cov
93%
_arrange_args
27.8
_kind_exporter
26.1
_do_export
25.6
CRAP 16.5
↑ Summary Build »
1 package Util::Any;
2  
3 58 ▶use ExportTo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 58 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 58 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 58 our $Utils = {
10 list => [ qw/List::Util List::MoreUtils List::Pairwise/ ],
11 data => [ qw/Scalar::Util/ ],
12 hash => [ qw/Hash::Util/ ],
13 debug => [ ['Data::Dumper', '', ['Dumper']] ],
14 string => [ qw/String::Util String::CamelCase/ ],
15 };
16  
17 58 $Utils->{'scalar'} = $Utils->{data};
18 # I'll delete no dash group in the above, in future.
19 58 $Utils->{'-' . $_} = $Utils->{$_} foreach keys %$Utils;
20  
21 58 our $SubExporterImport = 'do_import';
22  
23 ▶sub _default_kinds { }
Subroutine _default_kinds: called CC=1 SCAR=0.0
24  
25 # borrow from List::MoreUtils
26 sub _any (&@) {
27 92 ▶ my $f = shift;
Subroutine _any: called CC=4 SCAR=13.9
28 92 ▶ return if ! @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 17 75 92
29 75 for (@_) {
30 124 ▶ return 1 if $f->();
Branch: 1 branch
BranchTrueFalseTotal
if &$f() 38 86 124
31 }
32 37 return 0;
33 }
34  
35 sub _uniq (@) {
36 306 ▶ my %h;
Subroutine _uniq: called CC=2 SCAR=6.9
37 306 ▶ map { $h{$_}++ == 0 ? $_ : () } @_;
Branch: 1 branch
BranchTrueFalseTotal
$h{$_}++ == 0 ? : 5500 4 5504
38 }
39 # /end
40  
41 sub import {
42 124 ▶ my ($pkg, $caller) = (shift, (caller)[0]);
Subroutine import: called CC=21 SCAR=30.5
43 124 ▶ return $pkg->_base_import($caller, @_) if @_ and $_[0] =~/^-[A-Z]\w+$/o;
Branch: 1 branch
BranchTrueFalseTotal
if @_ and $_[0] =~ /^-[A-Z]\w+$/o 34 90 124
Condition100%3/3
@_ and $_[0] =~ /^-[A-Z]\w+$/o
!ll&&!rl&&r
48634
MC/DC100%2/2
@_ and $_[0] =~ /^-[A-Z]\w+$/o
@_ $_[0] =~ /^-[A-Z]\w+$/o
Truth table100%3/3
A and B
A= @_B= $_[0] =~ /^-[A-Z]\w+$/o
ABresult
0X0
100
111
44  
45 90 my %opt;
46 90 ▶ if (@_ > 1 and ref $_[-1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1 and ref $_[-1] eq "HASH") 42 48 90
Condition100%3/3
@_ > 1 and ref $_[-1] eq "HASH"
!ll&&!rl&&r
381042
MC/DC100%2/2
@_ > 1 and ref $_[-1] eq "HASH"
@_ > 1 ref $_[-1] eq "HASH"
Truth table100%3/3
A and B
A= @_ > 1B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
47 @opt{qw/prefix module_prefix debug smart_rename plugin/}
48 42 = (delete @{$_[-1]}{qw/prefix module_prefix debug smart_rename plugin/});
49 42 ▶ pop @_ unless %{$_[-1]};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$_[-1];} 31 11 42
50 }
51 90 ▶ $opt{$_} ||= 0 foreach qw/prefix module_prefix debug smart_rename/;
Condition100%2/2
$opt{$_} ||= 0
l!l
33327
MC/DC100%1/1
$opt{$_} ||= 0
$opt{$_}
Truth table100%2/2
A
A= $opt{$_}
Aresult
11
00
52 90 ▶ if (not defined $opt{plugin}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opt{'plugin'}) { } 82 8 90
elsif ($opt{'plugin'} and $opt{'plugin'} ne 'eager') { } 0 12 12
53 82 $opt{plugin} = 'lazy';
54 ▶ } elsif ($opt{plugin} and $opt{plugin} ne 'eager') {
Condition66%2/3
$opt{"plugin"} and $opt{"plugin"} ne "eager"
!ll&&!rl&&r
440
MC/DC0%0/2
$opt{"plugin"} and $opt{"plugin"} ne "eager"
$opt{"plugin"} $opt{"plugin"} ne "eager"
Truth table66%2/3
A and B
A= $opt{"plugin"}B= $opt{"plugin"} ne "eager"
ABresult
0X0
100
111
55 0 Carp::croak "wrong option is passed for plugin: " . $opt{plugin};
56 }
57  
58 90 ▶ @_ = %{$_[0]} if @_ == 1 and ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 7 83 90
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
36477
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
59  
60 58 ▶ my $config = Clone::clone(do { no strict 'refs'; ${$pkg . '::Utils'} });
Subroutine BEGIN: called CC=1 SCAR=0.0
61 90 ▶ if ($pkg->can('_plugins')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg->can("_plugins")) 13 77 90
62 13 ▶ if ($opt{plugin} eq 'eager') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"plugin"} eq "eager") 4 9 13
63 4 foreach my $plugin ($pkg->plugins) {
64 12 eval "require $plugin";
65 12 my $util = $plugin->utils;
66 12 foreach my $kind (keys %$util) {
67 16 ▶ push @{$config->{$kind} ||= []}, @{$util->{$kind}};
Condition100%2/2
$config->{$kind} ||= []
l!l
88
MC/DC100%1/1
$config->{$kind} ||= []
$config->{$kind}
Truth table100%2/2
A
A= $config->{$kind}
Aresult
11
00
68 }
69 }
70 }
71 }
72 90 ▶ my ($arg, $want_kind) = $pkg->_arrange_args
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 86 4 90
$_[0] =~ /^[-:]?all$/i ? : 20 66 86
73 ([
74 @_ ? ($_[0] =~m{^[-:]?all$}i ? ($_[0], $pkg->_default_kinds, @_[1 .. $#_]) : ($pkg->_default_kinds, @_))
75 : ($pkg->_default_kinds)
76 ],
77 $config, $caller, \%opt);
78 90 foreach my $kind (keys %$want_kind) {
79 # Carp::croak "$pkg doesn't have such kind of functions : $kind"
80 # unless exists $config->{$kind};
81 249 $pkg->_kind_exporter($caller, $config->{$kind}, (lc(join "", $kind =~m{(\w+)}go)), $want_kind->{$kind}, \%opt);
82 }
83 }
84  
85 sub _kind_exporter {
86 249 ▶ my ($pkg, $caller, $kind_config, $kind_word, $import_setting, $opt) = @_;
Subroutine _kind_exporter: called CC=26 SCAR=32.6
87 249 my ($wanted_funcs, $local_definition, $kind_prefix, $kind_args) = $pkg->_func_definitions($import_setting);
88 249 my ($prefix, %exported, %class_func);
89  
90 249 foreach my $class_config (@$kind_config) { # $class_config is class name or array ref
91 393 ▶ my ($class, $module_prefix, $config_options) = ref $class_config ? @$class_config : ($class_config, '', '');
Branch: 1 branch
BranchTrueFalseTotal
ref $class_config ? : 126 267 393
92  
93 393 my $evalerror = '';
94 393 ▶ if ($evalerror = do { local $@; eval {my $path = $class; $path =~s{::}{/}go; require $path. ".pm"; $evalerror = $@ }; $@}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($evalerror = do { local $@; eval { do { my $path = $class; $path =~ s[::][/]go; require $path . '.pm'; $evalerror = $@ } }; $@ }) 2 391 393
95 # if ($evalerror = do { local $@; eval "require $class"; $evalerror = $@ }) {
96 2 ▶ $opt->{debug} == 2 ? Carp::croak $evalerror : Carp::carp $evalerror;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'debug'} == 2 ? : 1 1 2
97 }
98  
99 $prefix = $kind_prefix ? $kind_prefix :
100 ($opt->{module_prefix} and $module_prefix) ? $module_prefix :
101 $opt->{prefix} ? lc($kind_word) . '_' :
102 392 ▶ $opt->{smart_rename} ? $pkg->_create_smart_rename($kind_word) : '';
Branch: 4 branches
BranchTrueFalseTotal
$kind_prefix ? : 27 365 392
$opt->{'module_prefix'} && $module_prefix ? : 8 357 365
$opt->{'prefix'} ? : 59 298 357
$opt->{'smart_rename'} ? : 1 297 298
Condition66%2/3
$opt->{"module_prefix"} and $module_prefix
!ll&&!rl&&r
35708
MC/DC50%1/2
$opt->{"module_prefix"} and $module_prefix
$opt->{"module_prefix"} $module_prefix
Truth table66%2/3
A and B
A= $opt->{"module_prefix"}B= $module_prefix
ABresult
0X0
100
111
103  
104 392 my (@funcs, %rename);
105 392 ▶ if (ref $config_options eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config_options eq 'HASH') { } 66 326 392
106 # -kind => {'first' => 'list_first', # first as list_first
107 # 'min' => \&build_min_reformatter,
108 # -select => ['first', 'sum', 'shuffle'] }
109  
110 66 ▶ if (exists $config_options->{-select}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $config_options->{'-select'}) { } 34 32 66
elsif (exists $config_options->{'-except'}) { } 1 31 32
elsif (not @funcs) { } 31 0 31
111 34 ▶ Carp::croak "cannot use -except & -select in same time." if exists $config_options->{-except};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config_options->{"-except"} 0 34 34
112 34 @funcs = @{$config_options->{-select}}
113 } elsif (exists $config_options->{-except}) {
114 1 my %except;
115 1 @except{@{$config_options->{-except}}} = ();
116 1 @funcs = grep !exists $except{$_}, @{_all_funcs_in_class($class)};
117 } elsif (not @funcs) {
118 31 @funcs = @{_all_funcs_in_class($class)};
119 }
120 66 ▶ foreach my $function (@$wanted_funcs ? (grep {defined $config_options->{$_}} @$wanted_funcs) : grep !/^-/, keys %$config_options) {
Branch: 1 branch
BranchTrueFalseTotal
@$wanted_funcs ? : 12 54 66
121 130 ▶ if (ref(my $gen = $config_options->{$function}) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref(my $gen = $config_options->{$function}) eq 'CODE') { } 83 47 130
elsif (defined &{$class . '::' . $function;}) { } 44 3 47
122 # Like Sub::Exporter generator
123 83 ▶ if (exists $local_definition->{$function}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $local_definition->{$function}) { } 6 77 83
124 6 foreach my $def (@{$local_definition->{$function}}) {
125 10 my %arg;
126 10 $arg{$_} = $def->{$_} for grep !/^-/, keys %$def;
127 10 ▶ ExportTo::export_to($caller => {($def->{-as} || $function)
Condition33%1/3
$def->{'-as'} || $function
l!l&&r!l&&!r
1000
MC/DC0%0/2
$def->{'-as'} || $function
$def->{'-as'} $function
Truth table33%1/3
A || B
A= $def->{'-as'}B= $function
ABresult
1X1
011
000
128 => $gen->($pkg, $class, $function, \%arg, $kind_args)});
129 }
130 } else {
131 77 ▶ if ($function ne '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($function ne '.') { } 76 1 77
132 76 ExportTo::export_to($caller => {$prefix . $function => $gen->($pkg, $class, $function, {}, $kind_args)});
133 } else {
134 1 $gen->($pkg, $class, $function, {}, $kind_args);
135 }
136 }
137 83 $exported{$function} = undef;
138 47 } elsif (defined &{$class . '::' . $function}) {
139 44 push @funcs, $function;
140 44 $rename{$function} = $config_options->{$function};
141 }
142 }
143 } else {
144 326 ▶ @funcs = ref $config_options eq 'ARRAY' ? @$config_options : @{_all_funcs_in_class($class)};
Branch: 1 branch
BranchTrueFalseTotal
ref $config_options eq 'ARRAY' ? : 42 284 326
145 }
146 392 $class_func{$class} = [\@funcs, \%rename];
147 }
148 248 my %want_funcs;
149 248 @want_funcs{@$wanted_funcs} = ();
150 248 foreach my $class (keys %class_func) {
151 _do_export($caller, $class, $class_func{$class}->[0], \%want_funcs, \%exported,
152 387 $local_definition, $class_func{$class}->[1], $prefix, $kind_prefix);
153 }
154 }
155  
156 sub _do_export {
157 387 ▶ my ($caller, $class, $funcs, $want_funcs, $exported, $local_definition, $rename, $prefix, $kind_prefix) = @_;
Subroutine _do_export: called CC=22 SCAR=32.4
158 387 my %reverse_rename = reverse %$rename;
159 387 ▶ if (%$local_definition) {
Branch: 2 branches
BranchTrueFalseTotal
if (%$local_definition) { } 29 358 387
elsif (@$funcs) { } 339 19 358
160 29 foreach my $func (keys %$local_definition) {
161 40 ▶ next if exists $exported->{$func};
Branch: 1 branch
BranchTrueFalseTotal
if exists $exported->{$func} 11 29 40
162 29 ▶ next if %$want_funcs and not exists $want_funcs->{$func};
Branch: 1 branch
BranchTrueFalseTotal
if %$want_funcs and not exists $want_funcs->{$func} 0 29 29
Condition66%2/3
%$want_funcs and not exists $want_funcs->{$func}
!ll&&!rl&&r
2450
MC/DC0%0/2
%$want_funcs and not exists $want_funcs->{$func}
%$want_funcs not exists $want_funcs->{$func}
Truth table66%2/3
A and not(B)
A= %$want_funcsB= not exists $want_funcs->{$func}
ABresult
0X0
100
111
163  
164 29 foreach my $def (@{$local_definition->{$func}}) {
165 29 ▶ if (ref $def eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $def eq 'HASH') { } 29 0 29
166 29 ▶ my $local_rename = $def->{-as} || '';
Condition50%1/2
$def->{'-as'} || ''
l!l
290
MC/DC0%0/1
$def->{'-as'} || ''
$def->{'-as'}
Truth table50%1/2
A
A= $def->{'-as'}
Aresult
11
00
167 29 ▶ my $original_func = $reverse_rename{$func} || $func;
Condition66%2/3
$reverse_rename{$func} || $func
l!l&&r!l&&!r
1280
MC/DC0%0/2
$reverse_rename{$func} || $func
$reverse_rename{$func} $func
Truth table66%2/3
A || B
A= $reverse_rename{$func}B= $func
ABresult
1X1
011
000
168 58 ▶ if (do { no strict 'refs'; defined &{$class . '::' . $original_func} }) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (do { no strict 'refs'; defined &{$class . '::' . $original_func;} }) 23 6 29
169 23 ▶ my $function_name =
Branch: 3 branches
BranchTrueFalseTotal
$local_rename ? : 23 0 23
$prefix ? : 0 0 0
ref $prefix eq 'CODE' ? : 0 0 0
170 ($local_rename ? $local_rename :
171 $prefix ? (ref $prefix eq 'CODE' ? $prefix->($func) : $prefix . $func) : $func);
172 23 ExportTo::export_to($caller => {$function_name => $class . '::' . $original_func});
173 }
174 } else {
175 0 ▶ Carp::croak("setting for fucntions must be hash ref for : $func => "
Branch: 1 branch
BranchTrueFalseTotal
ref $def eq 'ARRAY' ? : 0 0 0
176 . (ref $def eq 'ARRAY' ? "[". join(", ",@$def) ."]" : $def));
177 }
178 }
179 }
180 } elsif (@$funcs) {
181 58 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
182 339 @$funcs = grep defined &{$class . '::'. $_}, @$funcs;
183 339 ▶ return unless @$funcs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$funcs 0 339 339
184 }
185  
186 387 my @export_funcs = grep !exists $local_definition->{$_}, @$funcs;
187 387 ▶ @export_funcs = grep exists $want_funcs->{$_}, @export_funcs if %$want_funcs;
Branch: 1 branch
BranchTrueFalseTotal
if %$want_funcs 39 348 387
188 387 ▶ if ($prefix or %$rename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix or %$rename) { } 102 285 387
Condition100%3/3
$prefix or %$rename
l!l&&r!l&&!r
957285
MC/DC100%2/2
$prefix or %$rename
$prefix %$rename
Truth table100%3/3
A or B
A= $prefixB= %$rename
ABresult
1X1
011
000
189 102 ▶ if (ref $prefix eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prefix eq 'CODE') { } 1 101 102
190 1 ExportTo::export_to($caller => {map { $prefix->($_) => $class . '::' . $_} @export_funcs});
191 } else {
192 101 ▶ ExportTo::export_to($caller => {map { $prefix . ($rename->{$_} || $_) => $class . '::' . $_} @export_funcs});
Condition66%2/3
$rename->{$_} || $_
l!l&&r!l&&!r
7720550
MC/DC0%0/2
$rename->{$_} || $_
$rename->{$_} $_
Truth table66%2/3
A || B
A= $rename->{$_}B= $_
ABresult
1X1
011
000
193 }
194 } else {
195 285 ExportTo::export_to($caller => [map $class . '::' . $_, _uniq @export_funcs]);
196 }
197 }
198  
199 sub _create_smart_rename {
200 2 ▶ my ($pkg, $kind) = @_;
Subroutine _create_smart_rename: called CC=1 SCAR=0.0
201 return sub {
202 9 ▶ my $str = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
203 9 my $prefix = '';
204 9 ▶ if ($str =~s{^(is_|has_|enable_|disable_|isnt_|have_|set_)}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^(is_|has_|enable_|disable_|isnt_|have_|set_)//) 4 5 9
205 4 $prefix = $1;
206 }
207 9 ▶ if ($str !~ m{^$kind} and $str !~ m{$kind$}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $str =~ /^$kind/ || $str =~ /$kind$/) { } 5 4 9
Condition100%3/3
$str =~ /^$kind/ || $str =~ /$kind$/
l!l&&r!l&&!r
315
MC/DC100%2/2
$str =~ /^$kind/ || $str =~ /$kind$/
$str =~ /^$kind/ $str =~ /$kind$/
Truth table100%3/3
A || B
A= $str =~ /^$kind/B= $str =~ /$kind$/
ABresult
1X1
011
000
208 5 return $prefix . $kind . '_' . $str;
209 } else {
210 4 return $prefix . $str;
211 }
212 2 };
213 }
214  
215 {
216 58 my %tmp;
217 sub _all_funcs_in_class {
218 318 ▶ my ($class) = @_;
Subroutine _all_funcs_in_class: called CC=2 SCAR=6.9
219 318 ▶ return $tmp{$class} if exists $tmp{$class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $tmp{$class} 211 107 318
220 107 my %f;
221 {
222 58 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
223 107 @f{@{$class . '::EXPORT_OK'}, @{$class . '::EXPORT'}} = ();
224 }
225 107 return $tmp{$class} = [grep defined &{$class . '::' . $_}, keys %f];
226 }
227 }
228  
229 58 sub _arrange_args {
230 95 ▶ my ($pkg, $org_args, $config, $caller, $opt) = @_;
Subroutine _arrange_args: called CC=27 SCAR=33.2
231 95 my (@arg, %want_kind);
232 95 my $import_module = $pkg->_use_import_module;
233 95 my $all_improt = 0;
234 95 ▶ if (@$org_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$org_args) 92 3 95
235 92 ▶ @$org_args = %{$org_args->[0]} if ref $org_args->[0] and (ref $org_args->[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $org_args->[0] and ref $org_args->[0] eq "HASH" 0 92 92
Condition33%1/3
ref $org_args->[0] and ref $org_args->[0] eq "HASH"
!ll&&!rl&&r
9200
MC/DC0%0/2
ref $org_args->[0] and ref $org_args->[0] eq "HASH"
ref $org_args->[0] ref $org_args->[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $org_args->[0]B= ref $org_args->[0] eq "HASH"
ABresult
0X0
100
111
236 92 ▶ $opt->{'plugin'} ||= '';
Condition100%2/2
$opt->{"plugin"} ||= ""
l!l
839
MC/DC100%1/1
$opt->{"plugin"} ||= ""
$opt->{"plugin"}
Truth table100%2/2
A
A= $opt->{"plugin"}
Aresult
11
00
237 92 ▶ if ($org_args->[0] =~ /^([:-])?all/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($org_args->[0] =~ /^([:-])?all/i) { } 20 72 92
elsif ($opt->{'plugin'} eq 'lazy' and $pkg->can('_plugins')) { } 4 79 83
238 20 my $all_import = shift @$org_args;
239 20 my $inherit_all = $1;
240 20 ▶ $pkg->_lazy_load_plugins_all($config) if $opt->{'plugin'} eq 'lazy' and $pkg->can('_plugins');
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"plugin"} eq "lazy" and $pkg->can("_plugins") 1 19 20
Condition100%3/3
$opt->{"plugin"} eq "lazy" and $pkg->can("_plugins")
!ll&&!rl&&r
2171
MC/DC100%2/2
$opt->{"plugin"} eq "lazy" and $pkg->can("_plugins")
$opt->{"plugin"} eq "lazy" $pkg->can("_plugins")
Truth table100%3/3
A and B
A= $opt->{"plugin"} eq "lazy"B= $pkg->can("_plugins")
ABresult
0X0
100
111
241 # import all functions which Util::Any proxy
242 20 @want_kind{keys %$config} = ();
243 20 ▶ if ($inherit_all and $import_module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inherit_all and $import_module) 4 16 20
Condition100%3/3
$inherit_all and $import_module
!ll&&!rl&&r
6104
MC/DC100%2/2
$inherit_all and $import_module
$inherit_all $import_module
Truth table100%3/3
A and B
A= $inherit_allB= $import_module
ABresult
0X0
100
111
244 4 ▶ if ($import_module eq 'Exporter' or $import_module eq 'Exporter::Simple') {
Branch: 3 branches
BranchTrueFalseTotal
if ($import_module eq 'Exporter' or $import_module eq 'Exporter::Simple') { } 1 3 4
elsif ($import_module eq 'Sub::Exporter') { } 3 0 3
elsif ($import_module eq 'Perl6::Export::Attrs') { } 0 0 0
Condition66%2/3
$import_module eq "Exporter" or $import_module eq "Exporter::Simple"
l!l&&r!l&&!r
103
MC/DC50%1/2
$import_module eq "Exporter" or $import_module eq "Exporter::Simple"
$import_module eq "Exporter" $import_module eq "Exporter::Simple"
Truth table66%2/3
A or B
A= $import_module eq "Exporter"B= $import_module eq "Exporter::Simple"
ABresult
1X1
011
000
245 58 ▶ no strict 'refs'; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
246 1 ▶ push @arg, ':all' if ${$pkg . '::EXPORT_TAGS'}{"all"};
Branch: 1 branch
BranchTrueFalseTotal
if do { $pkg . '::EXPORT_TAGS' }->{"all"} 1 0 1
247  
248 } elsif ($import_module eq 'Sub::Exporter') {
249 3 push @arg, '-all';
250 } elsif ($import_module eq 'Perl6::Export::Attrs') {
251 0 push @arg, ':ALL';
252 }
253 }
254 ▶ } elsif ($opt->{'plugin'} eq 'lazy' and $pkg->can('_plugins')) {
Condition100%3/3
$opt->{"plugin"} eq "lazy" and $pkg->can("_plugins")
!ll&&!rl&&r
11574
MC/DC100%2/2
$opt->{"plugin"} eq "lazy" and $pkg->can("_plugins")
$opt->{"plugin"} eq "lazy" $pkg->can("_plugins")
Truth table100%3/3
A and B
A= $opt->{"plugin"} eq "lazy"B= $pkg->can("_plugins")
ABresult
0X0
100
111
255 4 $pkg->_lazy_load_plugins($config, $org_args);
256 }
257 92 ▶ if (_any {ref $_} @$org_args) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&_any(sub { ref $_; } , @$org_args)) { } 38 54 92
258 38 ▶ for (my $i = 0; $i < @$org_args; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$org_args) 57 38 95
259 57 my $kind = $org_args->[$i];
260 57 my $ref = ref $org_args->[$i + 1];
261 57 ▶ my $import_setting = $ref ? $org_args->[++$i] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 48 9 57
262 57 ▶ if ($ref eq 'ARRAY' and !@$import_setting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY" and not @$import_setting) 2 55 57
Condition100%3/3
$ref eq "ARRAY" and not @$import_setting
!ll&&!rl&&r
31242
MC/DC100%2/2
$ref eq "ARRAY" and not @$import_setting
$ref eq "ARRAY" not @$import_setting
Truth table100%3/3
A and not(B)
A= $ref eq "ARRAY"B= not @$import_setting
ABresult
0X0
100
111
263 2 $import_setting = [''];
264 }
265 57 _insert_want_arg($config, $kind, $import_setting, \%want_kind, \@arg);
266 }
267 } else {
268 # export specified kinds
269 54 foreach my $kind (@$org_args) {
270 45 _insert_want_arg($config, $kind, undef, \%want_kind, \@arg);
271 }
272 }
273 }
274 95 ▶ if ($import_module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($import_module) { } 11 84 95
275 11 ▶ $pkg->_do_base_import($import_module, $caller, \@arg) if @arg or !@$org_args;
Branch: 1 branch
BranchTrueFalseTotal
if @arg or not @$org_args 10 1 11
Condition100%3/3
@arg or not @$org_args
l!l&&r!l&&!r
821
MC/DC100%2/2
@arg or not @$org_args
@arg not @$org_args
Truth table100%3/3
A or not(B)
A= @argB= not @$org_args
ABresult
1X1
011
000
276 } else {
277 84 ▶ Carp::carp("unknown arguments: @arg") if @arg;
Branch: 1 branch
BranchTrueFalseTotal
if @arg 4 80 84
278 }
279 95 return \@arg, \%want_kind;
280 }
281  
282 sub _insert_want_arg {
283 # distinct arguments to want(for Util::Any) and args(for other).
284 102 ▶ my ($config, $kind, $import_setting, $want_kind, $arg) = @_;
Subroutine _insert_want_arg: called CC=3 SCAR=11.0
285 102 $kind = lc $kind;
286 102 ▶ if (exists $config->{$kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{$kind}) { } 88 14 102
287 88 $want_kind->{$kind} = $import_setting;
288 } else {
289 14 ▶ push @$arg, $kind, defined $import_setting ? $import_setting : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $import_setting ? : 4 10 14
290 }
291 }
292  
293 sub _lazy_load_plugins_all {
294 1 ▶ my ($pkg, $config) = @_;
Subroutine _lazy_load_plugins_all: called CC=5 SCAR=16.1
295 1 foreach my $plugin (@{$pkg->_plugins}) {
296 3 eval "require $plugin";
297 3 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
298 3 my $util = $plugin->utils;
299 3 foreach my $kind (keys %$util) {
300 4 ▶ push @{$config->{$kind} ||= []}, @{$util->{$kind}};
Condition100%2/2
$config->{$kind} ||= []
l!l
22
MC/DC100%1/1
$config->{$kind} ||= []
$config->{$kind}
Truth table100%2/2
A
A= $config->{$kind}
Aresult
11
00
301 }
302 }
303 }
304  
305 sub _lazy_load_plugins {
306 4 ▶ my ($pkg, $config, $org_args) = @_;
Subroutine _lazy_load_plugins: called CC=16 SCAR=27.8
307 4 my (@all, @kinds);
308 4 for my $i (0 .. $#{$org_args}) {
309 5 ▶ next if ref $org_args->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if ref $org_args->[$i] 0 5 5
310 5 my $k = $org_args->[$i];
311 5 $k =~ s{\W+}{}g;
312 5 $k =~ s{_}{::}g;
313 5 ▶ $k =~ s{^(.+)::all$}{$1|$1::\\w+} and push @all, $i;
Branch: 1 branch
BranchTrueFalseTotal
$k =~ s/^(.+)::all$/$1|$1::\\w+/ and push @all, $i 1 4 5
314 5 push @kinds, $k;
315 }
316 4 ▶ return unless @kinds;
Branch: 1 branch
BranchTrueFalseTotal
unless @kinds 0 4 4
317  
318 4 my $regex = "^${pkg}::Plugin::(?:". join("|", @kinds) . ')';
319 4 my $all_regex = '';
320 4 ▶ if (@all) {
Branch: 1 branch
BranchTrueFalseTotal
if (@all) 1 3 4
321 1 $org_args->[$_] =~s{_all$}{} for @all;
322 1 $all_regex = "^${pkg}::Plugin::(?:".join("|", map {m{(\w+)}} @{$org_args}[@all]). ')';
323 }
324 4 foreach my $plugin (@{$pkg->_plugins}) {
325 12 ▶ if ($plugin =~m{$regex\W}i or $plugin =~m{$regex$}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin =~ /$regex\W/i or $plugin =~ /$regex$/i) 7 5 12
Condition100%3/3
$plugin =~ /$regex\W/i or $plugin =~ /$regex$/i
l!l&&r!l&&!r
255
MC/DC100%2/2
$plugin =~ /$regex\W/i or $plugin =~ /$regex$/i
$plugin =~ /$regex\W/i $plugin =~ /$regex$/i
Truth table100%3/3
A or B
A= $plugin =~ /$regex\W/iB= $plugin =~ /$regex$/i
ABresult
1X1
011
000
326 7 eval "require $plugin";
327 7 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
328 7 my $util = $plugin->utils;
329 7 foreach my $kind (keys %$util) {
330 9 ▶ push @{$config->{$kind} ||= []}, @{$util->{$kind}};
Condition100%2/2
$config->{$kind} ||= []
l!l
45
MC/DC100%1/1
$config->{$kind} ||= []
$config->{$kind}
Truth table100%2/2
A
A= $config->{$kind}
Aresult
11
00
331 9 ▶ if ($all_regex and ($plugin =~ m{$all_regex\W}i or $plugin =~ m{$all_regex$}i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_regex and $plugin =~ /$all_regex\W/i || $plugin =~ /$all_regex$/i) 2 7 9
Condition83%5/6
$all_regex and $plugin =~ /$all_regex\W/i || $plugin =~ /$all_regex$/i
!ll&&!rl&&r
711
$plugin =~ /$all_regex\W/i or $plugin =~ /$all_regex$/i
l!l&&r!l&&!r
110
MC/DC33%1/3
$all_regex and $plugin =~ /$all_regex\W/i || $plugin =~ /$all_regex$/i
$all_regex $plugin =~ /$all_regex\W/i $plugin =~ /$all_regex$/i
Truth table50%2/4
A and B or C
A= $all_regexB= $plugin =~ /$all_regex\W/iC= $plugin =~ /$all_regex$/i
ABCresult
0XX0
1000
11X1
1011
332 2 push @$org_args, $kind;
333 }
334 }
335 }
336 }
337 }
338  
339 sub _func_definitions {
340 249 ▶ my ($pkg, $want_func_definition) = @_;
Subroutine _func_definitions: called CC=13 SCAR=25.7
341 249 my ($kind_prefix, $kind_args, @wanted_funcs, %funcs, %local_definition);
342 249 ▶ if (ref $want_func_definition eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $want_func_definition eq 'HASH') { } 17 232 249
elsif (ref $want_func_definition eq 'ARRAY') { } 21 211 232
343 # list => {func => {-as => 'rename'}}; list => {-prefix => 'hoge_' }
344 $kind_prefix = $want_func_definition->{-prefix}
345 17 ▶ if exists $want_func_definition->{-prefix};
Branch: 1 branch
BranchTrueFalseTotal
if exists $want_func_definition->{"-prefix"} 12 5 17
346 $kind_args = $want_func_definition->{-args}
347 17 ▶ if exists $want_func_definition->{-args};
Branch: 1 branch
BranchTrueFalseTotal
if exists $want_func_definition->{"-args"} 0 17 17
348 17 foreach my $f (grep !/^-/, keys %$want_func_definition) {
349 12 $local_definition{$f} = [$want_func_definition->{$f}];
350 }
351 } elsif (ref $want_func_definition eq 'ARRAY') {
352 21 ▶ foreach (my $i = 0; $i < @$want_func_definition; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$want_func_definition) 39 21 60
353 39 my ($k, $v) = @{$want_func_definition}[$i, $i + 1];
354 39 ▶ if ($k eq '-prefix') {
Branch: 3 branches
BranchTrueFalseTotal
if ($k eq '-prefix') { } 1 38 39
elsif ($k eq '-args') { } 1 37 38
elsif (ref $v) { } 13 24 37
355 1 $kind_prefix = $v;
356 1 $i++;
357 } elsif ($k eq '-args') {
358 1 $kind_args = $v;
359 1 $i++;
360 }elsif (ref $v) {
361 13 $i++;
362 13 push @wanted_funcs, $k;
363 13 ▶ push @{$local_definition{$k} ||= []}, $v;
Condition100%2/2
$local_definition{$k} ||= []
l!l
49
MC/DC100%1/1
$local_definition{$k} ||= []
$local_definition{$k}
Truth table100%2/2
A
A= $local_definition{$k}
Aresult
11
00
364 } else {
365 24 push @wanted_funcs, $k;
366 }
367 }
368 21 @wanted_funcs = _uniq @wanted_funcs;
369 }
370 249 ▶ return \@wanted_funcs, \%local_definition, $kind_prefix || '', $kind_args || {};
Condition100%4/4
$kind_prefix || ''
l!l
13236
$kind_args || {}
l!l
1248
MC/DC100%1/1
$kind_prefix || ''
$kind_prefix
MC/DC100%1/1
$kind_args || {}
$kind_args
Truth table100%2/2
A
A= $kind_prefix
Aresult
11
00
Truth table100%2/2
A
A= $kind_args
Aresult
11
00
371 }
372  
373 sub _do_base_import {
374 # working with other modules like Expoter
375 10 ▶ my ($pkg, $import_module, $caller, $arg) = @_;
Subroutine _do_base_import: called CC=7 SCAR=19.9
376 10 my $pkg_utils;
377 {
378 58 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
379 58 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
380 10 $pkg_utils = ${$pkg . '::Utils'};
381 }
382 10 ▶ if ($import_module eq 'Perl6::Export::Attrs') {
Branch: 4 branches
BranchTrueFalseTotal
if ($import_module eq 'Perl6::Export::Attrs') { } 0 10 10
elsif ($import_module eq 'Exporter::Simple') { } 0 10 10
elsif ($import_module eq 'Exporter') { } 4 6 10
elsif ($import_module eq 'Sub::Exporter') { } 6 0 6
383 0 eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import(@$arg);';
384 } elsif ($import_module eq 'Exporter::Simple') {
385 0 eval "package $caller; $pkg" . '->Exporter::Simple::import(@$arg);';
386 } elsif ($import_module eq 'Exporter') {
387 4 eval "package $caller; $pkg" . '->Exporter::import(@$arg);';
388 } elsif ($import_module eq 'Sub::Exporter') {
389 58 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
390 58 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
391 6 ▶ my $import_name = ${"${pkg}::SubExporterImport"} || $Util::Any::SubExporterImport;
Condition66%2/3
${"${pkg}::SubExporterImport";} || $Util::Any::SubExporterImport
l!l&&r!l&&!r
150
MC/DC0%0/2
${"${pkg}::SubExporterImport";} || $Util::Any::SubExporterImport
${"${pkg}::SubExporterImport";} $Util::Any::SubExporterImport
Truth table66%2/3
A || B
A= ${"${pkg}::SubExporterImport";}B= $Util::Any::SubExporterImport
ABresult
1X1
011
000
392 6 eval "package $caller; $pkg" . '->$import_name(@$arg);';
393 }
394 10 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 10 10
395 }
396  
397 sub _base_import {
398 42 ▶ my ($pkg, $caller, @flgs) = @_;
Subroutine _base_import: called CC=10 SCAR=23.0
399 {
400 58 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
401 42 push @{"${caller}::ISA"}, __PACKAGE__;
402 }
403 42 my @unknown;
404 42 ▶ while (@flgs and my $flg = lc shift @flgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flgs and my $flg = lc shift @flgs) 45 42 87
Condition66%2/3
@flgs and my $flg = lc shift @flgs
!ll&&!rl&&r
42045
MC/DC50%1/2
@flgs and my $flg = lc shift @flgs
@flgs my $flg = lc shift @flgs
Truth table66%2/3
A and B
A= @flgsB= my $flg = lc shift @flgs
ABresult
0X0
100
111
405 58 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
406 45 ▶ if ($flg eq '-perl6exportattrs') {
Branch: 6 branches
BranchTrueFalseTotal
if ($flg eq '-perl6exportattrs') { } 3 42 45
elsif ($flg eq '-subexporter') { } 8 34 42
elsif ($flg eq '-exportersimple') { } 5 29 34
elsif ($flg eq '-exporter') { } 7 22 29
elsif ($flg eq '-base') { } 18 4 22
elsif ($flg eq '-pluggable') { } 3 1 4
407 3 eval {require Perl6::Export::Attrs };
408 3 *{$caller . '::MODIFY_CODE_ATTRIBUTES'} = \&Perl6::Export::Attrs::_generic_MCA;
409 3 ▶ *{$caller . '::_use_import_module'} = sub { 'Perl6::Export::Attrs' };
Subroutine __ANON__: called CC=1 SCAR=0.0
410 } elsif ($flg eq '-subexporter') {
411 8 eval { require Sub::Exporter };
412 8 ▶ *{$caller . '::_use_import_module'} = sub { 'Sub::Exporter' };
Subroutine __ANON__: called CC=1 SCAR=0.0
413 } elsif ($flg eq '-exportersimple') {
414 5 eval { require Exporter::Simple };
415 5 ▶ *{$caller . '::_use_import_module'} = sub { 'Exporter::Simple' };
Subroutine __ANON__: called CC=1 SCAR=0.0
416 } elsif ($flg eq '-exporter') {
417 7 require Exporter;
418 7 push @{"${caller}::ISA"}, 'Exporter';
419 7 ▶ *{$caller . '::_use_import_module'} = sub { 'Exporter' };
Subroutine __ANON__: called CC=1 SCAR=0.0
420 } elsif ($flg eq '-base') {
421 # nothing to do
422 } elsif ($flg eq '-pluggable') {
423 # pluggable
424 3 require Module::Pluggable;
425 3 Module::Pluggable->import(require => 0, search_path => [$caller . '::Plugin'], inner => 0);
426 3 my @plugins = $pkg->plugins;
427 3 ▶ *{$caller . '::_plugins'} = sub { \@plugins };
Subroutine __ANON__: called CC=1 SCAR=0.0
428 } else {
429 1 push @unknown, $flg;
430 }
431 }
432 42 ▶ Carp::croak "cannot understand the option: @unknown" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 1 41 42
433 }
434  
435 84 ▶sub _use_import_module { 0 }
Subroutine _use_import_module: called CC=1 SCAR=0.0
436  
437 =head1 NAME
438  
439 Util::Any - to export any utilities and to create your own utility module
440  
441 =cut
442  
443 58 our $VERSION = '0.24';
444  
445 =head1 SYNOPSIS
446  
447 use Util::Any -list;
448 # you can import any functions of List::Util and List::MoreUtils
449
450 print uniq qw/1, 0, 1, 2, 3, 3/;
451  
452 If you want to choose functions
453  
454 use Util::Any -list => ['uniq'];
455 # you can import uniq function only, not import other functions
456
457 print uniq qw/1, 0, 1, 2, 3, 3/;
458  
459 If you want to import All kind of utility functions
460  
461 use Util::Any -all;
462
463 my $o = bless {};
464 my %hash = (a => 1, b => 2);
465
466 # from Scalar::Util
467 blessed $o;
468
469 # from Hash::Util
470 lock_keys %hash;
471  
472 If you want to import functions with prefix(ex. list_, scalar_, hash_)
473  
474 use Util::Any -all, {prefix => 1};
475 use Util::Any -list, {prefix => 1};
476 use Util::Any -list => ['uniq', 'min'], {prefix => 1};
477
478 print list_uniq qw/1, 0, 1, 2, 3, 3/;
479
480  
481 If you want to import functions with your own prefix.
482  
483 use Util::Any -list => {-prefix => "l_"};
484 print l_uniq qw/1, 0, 1, 2, 3, 3/;
485  
486 If you want to import functions as different name.
487  
488 use Util::Any -list => {uniq => {-as => 'listuniq'}};
489 print listuniq qw/1, 0, 1, 2, 3, 3/;
490  
491 When you use both renaming and your own prefix ?
492  
493 use Util::Any -list => {uniq => {-as => 'listuniq'}, -prefix => "l_"};
494 print listuniq qw/1, 0, 1, 2, 3, 3/;
495 print l_min qw/1, 0, 1, 2, 3, 3/;
496 # the following is NG
497 print l_uniq qw/1, 0, 1, 2, 3, 3/;
498  
499 =head1 DESCRIPTION
500  
501 For the people like the man who cannot remember C<uniq> function is in whether List::Util or List::MoreUtils.
502 And for the newbie who don't know where useful utilities is.
503  
504 Perl has many modules and they have many utility functions.
505 For example, List::Util, List::MoreUtils, Scalar::Util, Hash::Util,
506 String::Util, String::CamelCase, Data::Dumper etc.
507  
508 We, Perl users, have to memorize modules name and their functions name.
509 Using this module, you don't need to memorize modules name,
510 only memorize kinds of modules and functions name.
511  
512 And this module allows you to create your own utility module, easily.
513 You can create your own module and use this in the same way as Util::Any like the following.
514  
515 use YourUtil -list;
516  
517 see C<CREATE YOUR OWN Util::Any>, in detail.
518  
519 =head1 HOW TO USE
520  
521 =head2 use Util::Any (KIND)
522  
523 use Util::Any -list, -hash;
524  
525 Give list of kinds of modules. All functions in modules are exported.
526  
527 =head2 use Util::Any KIND => [FUNCTIONS], ...;
528  
529 NOTE THAT kind '-all', 'all' or ':all' cannot take this option.
530  
531 use Util::Any -list => ['uniq'], -hash => ['lock_keys'];
532  
533 Give hash whose key is kind and value is function names as array ref.
534 Selected functions are exported.
535  
536 you can write it as hash ref.
537  
538 use Util::Any {-list => ['uniq'], -hash => ['lock_keys']};
539  
540 =head2 use Util::Any ..., {OPTION => VALUE};
541  
542 Util::Any can take last argument as option, which should be hash ref.
543  
544 =over 4
545  
546 =item prefix => 1
547  
548 add kind prefix to function name.
549  
550 use Util::Any -list, {prefix => 1};
551
552 list_uniq(1,2,3,4,5); # it is List::More::Utils's uniq function
553  
554 =item module_prefix => 1
555  
556 see L<PREFIX FOR EACH MODULE>.
557 Uti::Any itself doesn't have such a definition.
558  
559 =item smart_rename => 1
560  
561 see L<SMART RENAME FOR EACH KIND>.
562  
563 =item plugin => 'lazy' / 'eager' / 0 (default is 'lazy')
564  
565 If utility module based on Util::Any has plugin,
566 Its plugins are loaded when related kind is specified(if kind name matches module name).
567 If you want to load all plugin on using module, give 'eager' to this option.
568 If you don't want to use plugin, set 0.
569  
570 use Util::Yours -kind, .... {plugin => 'eager'}; # all plugins are loaded
571 use Util::Yours -kind, .... {plugin => 0}; # disable plugin feature.
572 use Util::Yours -kind; # is equal {plugin => 'lazy'}
573  
574 Relation of kind name and plugin name is the following.
575  
576 for example, If you have the following modules.
577  
578 Util::Yours::Plugin::Date
579 Util::Yours::Plugin::DateTime
580 Util::Yours::Plugin::Net
581 Util::Yours::Plugin::Net::Amazon
582 Util::Yours::Plugin::Net::Twitter
583  
584 the following code:
585  
586 use Util::Yours -date; # Plugin::Date is loaded
587 use Util::Yours -datetime; # Plugin::DateTime is loaded
588 use Util::Yours -net; # Plugin::Net is loaded
589 use Util::Yours -net_amazon; # Plugin::Net::Amazon is loaded
590 use Util::Yours -net_all; # Plugin::Net and Plugin::Net::* is loaded
591  
592 C<_all> is special keyword. see L<"NOTE ABOUT all KEYWORD">.
593  
594 =item debug => 1/2
595  
596 Util::Any doesn't say anything when loading module fails.
597 If you pass debug value, warn or die.
598  
599 use Util::Any -list, {debug => 1}; # warn
600 use Util::Any -list, {debug => 2}; # die
601  
602 =back
603  
604 =head1 EXPORT
605  
606 Kinds of functions and list of exported functions are below.
607 Note that these modules and version are on my environment(Perl 5.8.4).
608 So, it must be different on your environment.
609  
610 =head2 -data
611  
612 NOTE THAT: its old name is 'scalar' (you can use the name, yet).
613  
614 from Scalar::Util (1.19)
615  
616 blessed
617 dualvar
618 isvstring
619 isweak
620 looks_like_number
621 openhandle
622 readonly
623 refaddr
624 reftype
625 set_prototype
626 tainted
627 weaken
628  
629 =head2 -hash
630  
631 from Hash::Util (0.05)
632  
633 hash_seed
634 lock_hash
635 lock_keys
636 lock_value
637 unlock_hash
638 unlock_keys
639 unlock_value
640  
641 =head2 -list
642  
643 from List::Util (1.19)
644  
645 first
646 max
647 maxstr
648 min
649 minstr
650 reduce
651 shuffle
652 sum
653  
654 from List::MoreUtils (0.21)
655  
656 after
657 after_incl
658 all
659 any
660 apply
661 before
662 before_incl
663 each_array
664 each_arrayref
665 false
666 first_index
667 first_value
668 firstidx
669 firstval
670 indexes
671 insert_after
672 insert_after_string
673 last_index
674 last_value
675 lastidx
676 lastval
677 mesh
678 minmax
679 natatime
680 none
681 notall
682 pairwise
683 part
684 true
685 uniq
686 zip
687  
688 from List::Pairwise (0.29)
689  
690 mapp
691 grepp
692 firstp
693 lastp
694 map_pairwise
695 grep_pairwise
696 first_pairwise
697 last_pairwise
698 pair
699  
700 =head2 -string
701  
702 from String::Util (0.11)
703  
704 crunch
705 define
706 equndef
707 fullchomp
708 hascontent
709 htmlesc
710 neundef
711 nospace
712 randcrypt
713 randword
714 trim
715 unquote
716  
717 from String::CamelCase (0.01)
718  
719 camelize
720 decamelize
721 wordsplit
722  
723 =head2 -debug
724  
725 from Data::Dumper (2.121)
726  
727 Dumper
728  
729 =head1 EXPORTING LIKE Sub::Exporter
730  
731 Like Sub::Exporter, Util::Any can export function name as you like.
732  
733 use Util::Yours -list => {-prefix => 'list__', miin => {-as => "lmin"}};
734  
735 functions in -list, are exported with prefix "list__" except 'min' and 'min' is exported as C<lmin>.
736  
737 =head1 PRIORITY OF THE WAYS TO CHANGE FUNCTION NAME
738  
739 There are some ways to change function name.
740 Their priority is the following.
741  
742 =over 4
743  
744 =item 1 rename
745  
746 -list => {uniq => {-as => 'luniq'}}
747  
748 =item 2 kind_prefix
749  
750 -list => {-prefix => list}
751  
752 =item 3 module_prefix
753  
754 Only if module's prefix is defined
755  
756 ..., {module_prefix => 1}
757  
758 =item 4 prefix
759  
760 ..., {prefix => 1}
761  
762 =item 5 smart_rename
763  
764 ..., {smart_rename => 1}
765  
766 =back
767  
768 I don't recommend to use 3, 4, 5 in same time, because it may confuse you.
769  
770 =over 4
771  
772 =item 3 + 4
773  
774 if module's prefix is defined in class(not defined in Util::Any), use 3, or use 4.
775  
776 =item 3 + 5
777  
778 3 or 5. reason is as same as the above.
779  
780 =item 3 + 4 + 5
781  
782 5 is ignored.
783  
784 =item 4 + 5
785  
786 5 is ignored.
787  
788 =back
789  
790 =head1 NOTE ABOUT all KEYWORD
791  
792 B<all> is special keyword, so it has some restriction.
793  
794 =head2 use module with 'all' cannot take its arguments
795  
796 use Util::Any -all; # or 'all', ':all'
797  
798 This cannot take sequential arguments for "all". For example;
799  
800 NG: use Util::Any -all => ['shuffle'];
801  
802 When sequential arguments is kind's, it's ok.
803  
804 use Util::Any -all, -list => ['unique'];
805  
806 =head2 -plugin_module_all cannot take its arguments
807  
808 use Util::Yours -plugin_name_all;
809  
810 This cannot take sequential arguments for it. For example:
811  
812 NG: use Util::Yours -plugin_name_all => ['some_function'];
813  
814 =head1 CREATE YOUR OWN Util::Any
815  
816 Just inherit Util::Any and define $Utils hash ref as the following.
817  
818 package Util::Yours;
819
820 use Clone qw/clone/;
821 use Util::Any -Base; # as same as use base qw/Util::Any/;
822 # If you don't want to inherit Util::Any setting, no need to clone.
823 our $Utils = clone $Util::Any::Utils;
824 push @{$Utils->{-list}}, qw/Your::Favorite::List::Utils/;
825
826 1;
827  
828 In your code;
829  
830 use Util::Yours -list;
831  
832 =head2 $Utils STRUCTURE
833  
834 =head3 overview
835  
836 $Utils => {
837 # simply put module names
838 -kind1 => [qw/Module1 Module2 ..../],
839 -# Module name and its prefix
840 -kind2 => [ [Module1 => 'module_prefix'], ... ],
841 # limit functions to be exported
842 -kind3 => [ [Module1, 'module_prefix', [qw/func1 func2/] ], ... ],
843 # as same as above except not specify modul prefix
844 -kind4 => [ [Module1, '', [qw/func1 func2/] ], ... ],
845 };
846  
847 =head3 Key must be lower character.
848  
849 NG $Utils = { LIST => [qw/List::Util/]};
850 OK $Utils = { list => [qw/List::Util/]};
851 OK $Utils = { -list => [qw/List::Util/]};
852 OK $Utils = { ':list' => [qw/List::Util/]};
853  
854 =head3 C<all> cannot be used for key.
855  
856 NG $Utils = { all => [qw/List::Util/]};
857 NG $Utils = { -all => [qw/List::Util/]};
858 NG $Utils = { ':all' => [qw/List::Util/]};
859  
860 =head3 Value is array ref which contained scalar or array ref.
861  
862 Scalar is module name. Array ref is module name and its prefix.
863  
864 $Utils = { list => ['List::Utils'] };
865 $Utils = { list => [['List::Utils', 'prefix_']] };
866  
867 see L<PREFIX FOR EACH MODULE>
868  
869 =head2 PREFIX FOR EACH MODULE
870  
871 If you want to import many modules and they have same function name.
872 You can specify prefix for each module like the following.
873  
874 use base qw/Util::Any/;
875
876 our $Utils = {
877 list => [['List::Util' => 'lu_'], ['List::MoreUtils' => 'lmu_']]
878 };
879  
880 In your code;
881  
882 use Util::Yours qw/list/, {module_prefix => 1};
883  
884 =head2 SMART RENAME FOR EACH KIND
885  
886 smart_rename option rename function name by a little smart way.
887 For example,
888  
889 our $Utils = {
890 utf8 => [['utf8', '',
891 {
892 is_utf8 => 'is_utf8',
893 upgrade => 'utf8_upgrade',
894 downgrade => 'downgrade',
895 }
896 ]],
897 };
898  
899 In this definition, use C<prefix => 1> is not good idea. If you use it:
900  
901 is_utf8 => utf8_is_utf8
902 utf8_upgrade => utf8_utf8_upgrade
903 downgrade => utf8_downgrade
904  
905 That's too bad. If you use C<smart_rename => 1> instead:
906  
907 is_utf8 => is_utf8
908 utf8_upgrade => utf8_upgrade
909 downgrade => utf8_downgrade
910  
911 rename rule is represented in _create_smart_rename in Util::Any.
912  
913 =head2 CHANGE smart_rename BEHAVIOUR
914  
915 To define _create_smart_rename, you can change smart_rename behaviour.
916 _create_smart_rename get 2 argument, package name and kind of utility,
917 and should return code reference which get function name and return new name.
918 As an example, see Util::Any's _create_smart_rename.
919  
920 =head2 OTHER WAY TO EXPORT FUNCTIONS
921  
922 =head3 SELECT FUNCTIONS
923  
924 Util::Any automatically export functions from modules' @EXPORT and @EXPORT_OK.
925 In some cases, it is not good idea like Data::Dumper's Dumper and C<DumperX>.
926 These 2 functions are same feature.
927  
928 So you can limit functions to be exported.
929  
930 our $Utils = {
931 -debug => [
932 ['Data::Dumper', '',
933 ['Dumper']], # only Dumper method is exported.
934 ],
935 };
936  
937 or
938  
939 our $Utils = {
940 -debug => [
941 ['Data::Dumper', '',
942 { -select => ['Dumper'] }, # only Dumper method is exported.
943 ]
944 ],
945 };
946  
947  
948 =head3 SELECT FUNCTIONS EXCEPT
949  
950 Inverse of -select option. Cannot use this option with -select.
951  
952 our $Utils = {
953 -debug => [
954 ['Data::Dumper', '',
955 { -except => ['DumperX'] }, # export functions except DumperX
956 ]
957 ],
958 };
959  
960 =head3 RENAME FUNCTIONS
961  
962 To rename function name, write original function name as hash key and renamed name as hash value.
963 this definition is prior to -select/-except.
964  
965 In the following example, 'min' is not in -select list, but can be exported.
966  
967 our $Utils = {
968 -list => [[
969 'List::Util', '',
970 {
971 'first' => 'list_first', # first as list_first
972 'sum' => 'lsum', # sum as lsum
973 'min' => 'lmin', # min as lmin
974 -select => ['first', 'sum', 'shuffle'],
975 }
976 ]]
977 };
978  
979 =head3 USE Sub::Exporter's GENERATOR WAY
980  
981 It's somewhat complicate, I just show you code.
982  
983 Your utility class:
984  
985 package SubExporterGenerator;
986
987 use strict;
988 use Util::Any -Base;
989
990 our $Utils =
991 {
992 -test => [[
993 'List::Util', '',
994 { min => \&build_min_reformatter,}
995 ]]
996 };
997
998 sub build_min_reformatter {
999 my ($pkg, $class, $name, @option) = @_;
1000 no strict 'refs';
1001 my $code = do { no strict 'refs'; \&{$class . '::' . $name}};
1002 sub {
1003 my @args = @_;
1004 $code->(@args, $option[0]->{under} || ());
1005 }
1006 }
1007  
1008 Your script using your utility class:
1009  
1010 package main;
1011
1012 use strict;
1013 use lib qw(lib t/lib);
1014 use SubExporterGenerator -test => [
1015 min => {-as => "min_under_20", under => 20},
1016 min => {-as => "min_under_5" , under => 5},
1017 ];
1018
1019 print min_under_20(100,25,30); # 20
1020 print min_under_20(100,10,30); # 10
1021 print min_under_20(100,25,30); # 5
1022 print min_under_20(100,1,30); # 1
1023  
1024 If you don't specify C<-as>, exported function as C<min>.
1025 But, of course, the following doesn't work.
1026  
1027 use SubExporterGenerator -test => [
1028 min => {under => 20},
1029 min => {under => 5},
1030 ];
1031  
1032 Util::Any try to export duplicate function C<min>, one of both should fail.
1033  
1034 =head4 GIVE DEFAULT ARGUMENTS TO CODE GENERATOR
1035  
1036 You may want to give default arguments to all code generators in same kind.
1037 For example, if you create shortcut to use Number::Format,
1038 you may want to give common arguments with creating instance.
1039  
1040 -number => [
1041 [ 'Number::Format' => {
1042 'round' => sub {
1043 my($pkg, $class, $func, $args, $default_args) = @_;
1044 my $n = 'Number::Format'->new(%$default_args);
1045 sub { $n->round(@_); }
1046 },
1047 'number_format' => sub {
1048 my($pkg, $class, $func, $args, $default_args) = @_;
1049 my $n = 'Number::Format'->new(%$default_args, %$args);
1050 sub { $n->format_number(@_); }
1051 }
1052 }
1053 ];
1054  
1055 And write as the following:
1056  
1057 use Util::Yours -number => [-args => {thousands_sep => "_", int_curr_symbol => '\'} ];
1058
1059 print number_format(100000); # 100_000
1060 print number_price(100000); # \100_000
1061  
1062 thousands_sep and int_curr_symbol are given to all of -number kind of function.
1063  
1064 =head2 DO SOMETHING WITHOUT EXPORTING ANYTHING
1065  
1066 -strict => [
1067 [ 'strict' => {
1068 '.' => sub {
1069 strict->import();
1070 warnings->import();
1071 },
1072 }
1073 ];
1074  
1075 This definition works like as pragma.
1076  
1077 use Util::Yours -strict;
1078  
1079 function name '.' is special. This name is not exported and only execute the code in the definition.
1080  
1081 =head2 ADD DEFAULT ARGUMENT FOR EXPORTING
1082  
1083 Define the following method.
1084  
1085 package You::Utils -Base;
1086 # ....
1087 sub _default_kinds { '-list', '-string' }
1088  
1089 This means '-list' and '-string' arguments are given as default exporting arguments.
1090 So, these are same.
1091  
1092 use Your::Utils;
1093  
1094 is equal to
1095  
1096 use Your::Utils -list, -string;
1097  
1098 If you want to disable default kinds.
1099  
1100 use Your::Utils -list => [], -string;
1101  
1102 =head2 ADD PLUGGABLE FEATURE FOR YOUR MODULE
1103  
1104 Just add a flag -Pluggbale.
1105  
1106 package Util::Yours;
1107 use Util::Any -Base, -Pluggable;
1108  
1109 And write plugin as the following:
1110  
1111 package Util::Yours::Plugin::Net;
1112
1113 sub utils {
1114 # This structure is as same as $Utils.
1115 return {
1116 # kind name and plugin name should be same.
1117 -net => [
1118 [
1119 'Net::Amazon', '',
1120 {
1121 amazon => sub {
1122 my ($pkg, $class, $func, $args) = @_;
1123 my $amazon = Net::Amazon->new(token => $args->{token});
1124 sub { $amazon }
1125 },
1126 }
1127 ]
1128 ]
1129 };
1130 }
1131
1132 1;
1133  
1134 And you can use it as the following.
1135  
1136 use Util::Yours -net => [amazon => {token => "your_token"}];
1137
1138 my $amazon = amazon; # get Net::Amazon object;
1139  
1140 Util::Any can merge definition in plugins. If same kind is in several plugins, it works.
1141 But same kind and same function name is defined, one of them doesn't work.
1142  
1143 =head2 WORKING WITH EXPORTER-LIKE MODULES
1144  
1145 NOTE THAT: I don't recommend this usage, because using this may confuse user;
1146 some of import options are for Util::Any and others are for exporter-like module
1147 (especially, using with Sub::Exporter is confusing).
1148  
1149 CPAN has some modules to export functions.
1150 Util::Any can work with some of such modules, L<Exporter>, L<Exporter::Simple>, L<Sub::Exporter> and L<Perl6::Export::Attrs>.
1151 If you want to use other modules, please inform me or implement import method by yourself.
1152  
1153 If you want to use module mentioned above, you have to change the way to inherit these modules.
1154  
1155 =head3 DIFFERENCE between 'all' and '-all' or ':all'
1156  
1157 If your utility module which inherited Util::Any has utility functions and export them by Exporter-like module,
1158 behaviour of 'all' and '-all' or ':all' is a bit different.
1159  
1160 'all' ... export all utilities defined in your package's $Utils variables.
1161 '-all' or ':all' ... export all utilities including functions in your util module itself.
1162  
1163 =head3 ALTERNATIVE INHERITING
1164  
1165 Normally, you use;
1166  
1167 package YourUtils;
1168
1169 use Util::Any -Base; # or "use base qw/Util::Any/;"
1170  
1171 But, if you want to use L<Exporter>, L<Exporter::Simple> or L<Perl6::Export::Attrs>.
1172 write as the following, instead.
1173  
1174 # if you want to use Exporter
1175 use Util::Any -Exporter;
1176 # if you want to use Exporter::Simple
1177 use Util::Any -ExporterSimple;
1178 # if you want to use Sub::Exporter
1179 use Util::Any -SubExporter;
1180 # if you want to use Perl6::Export::Attrs
1181 use Util::Any -Perl6ExportAttrs;
1182  
1183 That's all.
1184 Note that B<don't use base the above modules in your utility module>.
1185  
1186 There is one notice to use Sub::Exporter.
1187  
1188 Sub::Exporter::setup_exporter
1189 ({
1190 as => 'do_import', # name is important
1191 exports => [...],
1192 groups => { ... },
1193 });
1194  
1195 You must pass "as" option to setup_exporter and its value must be "do_import".
1196 If you want to change this name, do the following.
1197  
1198 Sub::Exporter::setup_exporter
1199 ({
1200 as => $YourUtils::SubExporterImport = '__do_import',
1201 exports => [...],
1202 groups => { ... },
1203 });
1204  
1205 =head3 EXAMPLE to USE Perl6::Export::Attrs in YOUR OWN UTIL MODULE
1206  
1207 Perl6::Export::Attributes is not recommended in the following URL
1208 (http://www.perlfoundation.org/perl5/index.cgi?pbp_module_recommendation_commentary).
1209 So, you'd better use other exporter module. It is left as an example.
1210  
1211 package Util::Yours;
1212
1213 use Clone qw/clone/;
1214 use Util::Any -Perl6ExportAttrs;
1215 our $Utils = clone $Util::Any::Utils;
1216 push @{$Utils->{list}}, qw/Your::Favorite::List::Utils/;
1217
1218 sub foo :Export(:DEFAULT) {
1219 return "foo!";
1220 }
1221
1222 sub bar :Export(:bar) {
1223 return "bar!";
1224 }
1225
1226 1;
1227  
1228 =head2 IMPLEMENT IMPORT by YOURSELF
1229  
1230 Perl6::Export::Attributes is not recommended in the following URL
1231 (http://www.perlfoundation.org/perl5/index.cgi?pbp_module_recommendation_commentary).
1232 So, you'd better use other exporter module. It is left as an example.
1233  
1234 You can write your own import method and BEGIN block like the following.
1235 Instead of using "use Util::Any -Perl6ExportAttrs".
1236  
1237 package UtilPerl6ExportAttr;
1238
1239 use strict;
1240 use base qw/Util::Any/;
1241 use Clone qw/clone/;
1242
1243 BEGIN {
1244 use Perl6::Export::Attrs ();
1245 no strict 'refs';
1246 *{__PACKAGE__ . '::MODIFY_CODE_ATTRIBUTES'} = \&Perl6::Export::Attrs::_generic_MCA;
1247 }
1248
1249 our $Utils = clone $Util::Any::Utils;
1250 $Utils->{your_list} = [
1251 ['List::Util', '', [qw(first min sum)]],
1252 ];
1253
1254 sub import {
1255 my $pkg = shift;
1256 my $caller = (caller)[0];
1257
1258 no strict 'refs';
1259 eval "package $caller; $pkg" . '->Util::Any::import(@_);';
1260 my @arg = grep !exists $Utils->{$_}, @_;
1261 if ((@_ and @arg) or !@_) {
1262 eval "package $caller; $pkg" . '->Perl6::Export::Attrs::_generic_import(@arg)';
1263 }
1264 return;
1265 }
1266
1267 sub foo :Export(:DEFAULT) {
1268 return "foo!";
1269 }
1270
1271 1;
1272  
1273 =head1 AUTHOR
1274  
1275 Ktat, C<< <ktat at cpan.org> >>
1276  
1277 =head1 BUGS
1278  
1279 Please report any bugs or feature requests to
1280 C<bug-util-any at rt.cpan.org>, or through the web interface at
1281 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Util-Any>.
1282 I will be notified, and then you'll automatically be notified of progress on
1283 your bug as I make changes.
1284  
1285 =head1 SUPPORT
1286  
1287 You can find documentation for this module with the perldoc command.
1288  
1289 perldoc Util::Any
1290  
1291 You can also look for information at:
1292  
1293 =over 4
1294  
1295 =item * AnnoCPAN: Annotated CPAN documentation
1296  
1297 L<http://annocpan.org/dist/Util-Any>
1298  
1299 =item * CPAN Ratings
1300  
1301 L<http://cpanratings.perl.org/d/Util-Any>
1302  
1303 =item * RT: CPAN's request tracker
1304  
1305 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Util-Any>
1306  
1307 =item * Search CPAN
1308  
1309 L<http://search.cpan.org/dist/Util-Any>
1310  
1311 =back
1312  
1313 =head1 REPOSITORY
1314  
1315 svn co http://svn.coderepos.org/share/lang/perl/Util-Any/trunk Util-Any
1316  
1317 Subversion repository of Util::Any is hosted at http://coderepos.org/share/.
1318 patches and collaborators are welcome.
1319  
1320 =head1 SEE ALSO
1321  
1322 The following modules can work with Util::Any.
1323  
1324 L<Exporter>, L<Exporter::Simple>, L<Sub::Exporter> and L<Perl6::Export::Attrs>.
1325  
1326 The following is new module Util::All, based on Util::Any.
1327  
1328 http://github.com/ktat/Util-All
1329  
1330 =head1 ACKNOWLEDGEMENTS
1331  
1332 =head1 COPYRIGHT & LICENSE
1333  
1334 Copyright 2008-2010 Ktat, all rights reserved.
1335  
1336 This program is free software; you can redistribute it and/or modify it
1337 under the same terms as Perl itself.
1338  
1339 =cut
1340  
1341 58 1; # End of Util-Any
↑ Summary Build »