blib/lib/Class/Easy.pm

filter: Statementstmt 85.2% 127 / 149 Branchbran 66.6% 40 / 60 Conditioncond 44.4% 12 / 27 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 71.4% 15 / 21 Podpod 100% 12 / 12 totaltotal 72.7% 208 / 286 CC 46 SCAR 21.2
CC
46
Cov
76%
make_accessor
17.6
list_all_subs_for
10.6
__ANON__
8.7
CRAP 8.3
« blib/lib/Class/Easy/Log.pm ↑ Summary blib/lib/Class/Easy/Base.pm »
1 package Class::Easy;
2  
3 # PORTIONS FROM Sub::Identify and common::sense
4  
5 5 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=18.9
6 5 our $VERSION = '0.18';
7 5 our @ISA;
8  
9 6 ▶ use Class::Easy::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 5 my $loaded;
12 5 ▶ unless ($ENV{PERL_SUB_IDENTIFY_PP}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"PERL_SUB_IDENTIFY_PP"}) 5 0 5
13 5 local $@;
14 5 eval {
15 5 require XSLoader;
16 5 XSLoader::load(__PACKAGE__, $VERSION);
17 };
18
19 5 ▶ die $@ if $@ && $@ !~ /object version|loadable object/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $@ =~ /object version|loadable object/ 0 5 5
Condition33%1/3
$@ and not $@ =~ /object version|loadable object/
!ll&&!rl&&r
500
MC/DC0%0/2
$@ and not $@ =~ /object version|loadable object/
$@ not $@ =~ /object version|loadable object/
Truth table33%1/3
A and not(B)
A= $@B= not $@ =~ /object version|loadable object/
ABresult
0X0
100
111
20
21 5 ▶ $loaded = 1 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 5 0 5
22 }
23
24 5 our $is_pure_perl = !$loaded;
25
26 5 ▶ if ($is_pure_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Class::Easy::is_pure_perl) 0 5 5
27 0 require Class::Easy::PP;
28 }
29  
30 }
31  
32 5 require Class::Easy::Timer;
33  
34 0 ▶sub stash_name ($) { (get_coderef_info($_[0]))[0] }
Subroutine stash_name: not called CC=1 SCAR=6.9
Pod documented
35 0 ▶sub sub_name ($) { (get_coderef_info($_[0]))[1] }
Subroutine sub_name: not called CC=1 SCAR=6.9
Pod documented
36 0 ▶sub sub_fullname ($) { join '::', get_coderef_info($_[0]) }
Subroutine sub_fullname: not called CC=1 SCAR=6.9
Pod documented
37  
38  
39 5 our @EXPORT = qw(has try_to_use try_to_use_quiet try_to_use_inc try_to_use_inc_quiet make_accessor timer);
40 5 our @EXPORT_OK = qw(sub_name stash_name sub_fullname get_coderef_info);
41  
42 5 our %EXPORT_FOREIGN = (
43 'Class::Easy::Log' => [qw(debug critical debug_depth logger catch_stderr release_stderr)],
44 # 'Class::Easy::Timer' => [qw(timer)],
45 );
46  
47 5 our $LOG = '';
48  
49 3 ▶sub timer {
Subroutine timer: called CC=1 SCAR=0.0
Pod documented
50 3 return Class::Easy::Timer->new (@_);
51 }
52  
53 4 ▶sub import {
Subroutine import: called CC=6 SCAR=18.3
54 4 my $mypkg = shift;
55 4 my $callpkg = caller;
56
57 4 my %params = @_;
58
59 # use warnings
60 4 ${^WARNING_BITS} = $Class::Easy::Import::WARN;
61
62 # use strict, use utf8;
63 4 $^H |= $Class::Easy::Import::H;
64
65 # use feature
66 4 $^H{feature_switch} = $^H{feature_say} = $^H{feature_state} = 1;
67
68 # probably check for try_to_use is enough
69 return
70 4 if defined *{"$callpkg\::try_to_use"}{CODE}
71 0 ▶ and sub_fullname (*{"$callpkg\::try_to_use"}{CODE}) eq __PACKAGE__.'::__ANON__';
Branch: 1 branch
BranchTrueFalseTotal
if defined *{"${callpkg}::try_to_use";}{"CODE"} and &sub_fullname(scalar *{"${callpkg}::try_to_use";}{"CODE"}) eq "Class::Easy::__ANON__" 0 4 4
Condition33%1/3
defined *{"${callpkg}::try_to_use";}{"CODE"} and &sub_fullname(scalar *{"${callpkg}::try_to_use";}{"CODE"}) eq "Class::Easy::__ANON__"
!ll&&!rl&&r
400
MC/DC0%0/2
defined *{"${callpkg}::try_to_use";}{"CODE"} and &sub_fullname(scalar *{"${callpkg}::try_to_use";}{"CODE"}) eq "Class::Easy::__ANON__"
defined *{"${callpkg}::try_to_use";}{"CODE"} &sub_fullname(scalar *{"${callpkg}::try_to_use";}{"CODE"}) eq "Class::Easy::__ANON__"
Truth table33%1/3
A and B
A= defined *{"${callpkg}::try_to_use";}{"CODE"}B= &sub_fullname(scalar *{"${callpkg}::try_to_use";}{"CODE"}) eq "Class::Easy::__ANON__"
ABresult
0X0
100
111
72
73 # export subs
74 4 *{"$callpkg\::$_"} = \&{"$mypkg\::$_"} foreach @EXPORT;
75 4 foreach my $p (keys %EXPORT_FOREIGN) {
76 4 *{"$callpkg\::$_"} = \&{"$p\::$_"} foreach @{$EXPORT_FOREIGN{$p}};
77 }
78 }
79  
80 2 ▶sub has ($;%) {
Subroutine has: called CC=1 SCAR=0.0
Pod documented
81
82 2 my ($caller) = caller;
83 2 my $accessor = shift;
84
85 2 return make_accessor ($caller, $accessor, _unless_exists => 1, @_);
86 }
87  
88 27 ▶sub make_accessor ($;$;$;%) {
Subroutine make_accessor: called CC=15 SCAR=28.7
Pod documented
89 27 my $caller = shift;
90 27 my $name = shift;
91  
92 27 my $full_ref = "${caller}::$name";
93
94 27 my $default;
95 27 ▶ $default = pop
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 or @_ == 3 0 27 27
Condition33%1/3
@_ == 1 or @_ == 3
l!l&&r!l&&!r
0027
MC/DC0%0/2
@_ == 1 or @_ == 3
@_ == 1 @_ == 3
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 3
ABresult
1X1
011
000
96 if @_ == 1 or @_ == 3; # _from_has support
97
98 27 ▶ die 'bad call from: ' . join (', ', caller)
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 0 27 27
99 if scalar @_ % 2;
100 27 my %config = @_;
101
102 27 my $isa = $config{isa};
103 27 ▶ my $is = $config{is} || 'ro';
Condition100%2/2
$config{'is'} || 'ro'
l!l
126
MC/DC100%1/1
$config{'is'} || 'ro'
$config{'is'}
Truth table100%2/2
A
A= $config{'is'}
Aresult
11
00
104 $default = $config{default}
105 27 ▶ if exists $config{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config{"default"} 10 17 27
106
107 27 ▶ $config{global} = 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $default and $is eq "ro" 10 17 27
Condition66%2/3
defined $default and $is eq "ro"
!ll&&!rl&&r
17010
MC/DC50%1/2
defined $default and $is eq "ro"
defined $default $is eq "ro"
Truth table66%2/3
A and B
A= defined $defaultB= $is eq "ro"
ABresult
0X0
100
111
108 if defined $default and $is eq 'ro';
109
110 # when make_accessor called from has, we must check for already created
111 # accessor and redefine only if redefined flag supplied
112 27 ▶ if (delete $config{_unless_exists} and defined *{$full_ref}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $config{"_unless_exists"} and defined *{$full_ref;}{"CODE"}) 0 27 27
Condition66%2/3
delete $config{"_unless_exists"} and defined *{$full_ref;}{"CODE"}
!ll&&!rl&&r
2520
MC/DC0%0/2
delete $config{"_unless_exists"} and defined *{$full_ref;}{"CODE"}
delete $config{"_unless_exists"} defined *{$full_ref;}{"CODE"}
Truth table66%2/3
A and B
A= delete $config{"_unless_exists"}B= defined *{$full_ref;}{"CODE"}
ABresult
0X0
100
111
113 0 return;
114 }
115
116 27 my $mode;
117 27 ▶ $mode = 1 if $is eq 'ro';
Branch: 1 branch
BranchTrueFalseTotal
if $is eq "ro" 26 1 27
118 27 ▶ $mode = 2 if $is eq 'rw';
Branch: 1 branch
BranchTrueFalseTotal
if $is eq "rw" 1 26 27
119
120 27 ▶ die "unknown accessor type: $is"
Branch: 1 branch
BranchTrueFalseTotal
unless $is =~ /^r[ow]$/ 0 27 27
121 unless $is =~ /^r[ow]$/;
122
123 27 ▶ if (ref $default eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $default eq 'CODE') { } 8 19 27
elsif ($config{'global'}) { } 2 17 19
124
125 8 *{$full_ref} = $default;
126
127 } elsif ($config{global}) {
128
129 2 ▶ *{$full_ref} = sub {
Subroutine __ANON__: called CC=4 SCAR=21.7
130
131 2 my $c = @_;
132
133 # return &$default if $c == 1 and ref $default eq 'CODE';
134 2 ▶ return $default if $c == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $c == 1 2 0 2
135 0 ▶ _has_error ($caller, $name, $c - 1) if $c ^ $mode;
Branch: 1 branch
BranchTrueFalseTotal
if $c ^ $mode 0 0 0
136
137 0 ▶ make_accessor (ref $_[0] || $_[0], $name, %config, default => $_[1]);
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
138 2 };
139
140 } else {
141 17 ▶ *{$full_ref} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
142
143 7 my $c = @_;
144
145 7 ▶ return $_[0]->{$name} if $c == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $c == 1 4 3 7
146 3 ▶ _has_error ($caller, $name, $c - 1) if $c ^ $mode;
Branch: 1 branch
BranchTrueFalseTotal
if $c ^ $mode 1 2 3
147
148 2 $_[0]->{$name} = $_[1];
149  
150 17 };
151
152 }
153 }
154  
155 1 ▶sub _has_error {
Subroutine _has_error: called CC=1 SCAR=0.0
156 1 my $caller = shift;
157 1 my $name = shift;
158 1 my $argc = shift;
159
160 1 my ($acc_caller, $line) = (caller(1))[0, 2];
161 1 die "too many parameters ($argc) for accessor $caller\->$name at $acc_caller line $line.\n";
162 }
163  
164 11 ▶sub _try_to_use {
Subroutine _try_to_use: called CC=6 SCAR=19.2
165 11 my $use_lib = shift;
166 11 my $quiet = shift;
167 11 my @chunks = @_;
168  
169 11 my $package = join '::', @chunks;
170 11 @chunks = split '::', $package;
171 11 my $path = join ('/', @chunks) . '.pm';
172
173 11 $@ = '';
174
175 11 ▶ if ($use_lib) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_lib) { } 0 11 11
176 return "exists in \%INC"
177 0 ▶ if exists $INC{$path};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{$path} 0 0 0
178 } else {
179 # OLD: we removed "or ! exists $INC{$path}" statement because
180 # "used" package always available via symbol table
181 11 ▶ if (eval ("scalar grep {!/\\w+\:\:/} keys \%$package\::;") > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "scalar grep {!/\\w+::/} keys %${package}::;" > 0) 0 11 11
182 0 return "exists in symbol table";
183 }
184 }
185
186 0 ▶ eval "use $package";
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
187
188 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 6 5 11
189 6 ▶ Class::Easy::Log::debug ("i can't load module ($path): $@")
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 6 0 6
190 unless $quiet;
191 6 return;
192 }
193
194 5 return 1;
195 }
196  
197 11 ▶sub try_to_use {
Subroutine try_to_use: called CC=1 SCAR=0.0
Pod documented
198 11 return _try_to_use (0, 0, @_);
199 }
200  
201 0 ▶sub try_to_use_quiet {
Subroutine try_to_use_quiet: not called CC=1 SCAR=6.9
Pod documented
202 0 return _try_to_use (0, 1, @_);
203 }
204  
205 0 ▶sub try_to_use_inc {
Subroutine try_to_use_inc: not called CC=1 SCAR=6.9
Pod documented
206 0 return _try_to_use (1, 0, @_);
207 }
208  
209 0 ▶sub try_to_use_inc_quiet {
Subroutine try_to_use_inc_quiet: not called CC=1 SCAR=6.9
Pod documented
210 0 return _try_to_use (1, 1, @_);
211 }
212  
213 2 ▶sub list_local_subs_for {
Subroutine list_local_subs_for: called CC=6 SCAR=17.9
Pod documented
214 2 my $module = shift;
215 2 ▶ my $enum_imported = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
216
217 2 my $namespace = \%{$module . '::'};
218
219 my @sub_list = grep {
220 31 defined *{"$module\::$_"}{CODE}
221 2 } keys %{$namespace};
222
223 2 my $sub_by_type = {
224 method => {},
225 imported => {},
226 runtime => {}
227 };
228
229 2 foreach my $sub (@sub_list) {
230 23 my ($real_package, $real_sub) = (get_coderef_info (*{"$module\::$sub"}{CODE}));
231  
232 23 ▶ if ($real_package eq $module) {
Branch: 2 branches
BranchTrueFalseTotal
if ($real_package eq $module) { } 6 17 23
elsif ($real_sub eq '__ANON__') { } 4 13 17
233 6 $sub_by_type->{method}->{$sub} = 1;
234 } elsif ($real_sub eq '__ANON__') {
235 4 $sub_by_type->{runtime}->{$sub} = 1;
236 } else {
237 13 $sub_by_type->{imported}->{$real_package}->{$real_sub} = $sub; # who needs $real_sub ?
238 }
239 }
240
241 wantarray
242 2 ▶ ? (keys %{$sub_by_type->{method}}, keys %{$sub_by_type->{runtime}})
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
243 : $sub_by_type;
244 }
245  
246 1 ▶sub list_all_subs_for {
Subroutine list_all_subs_for: called CC=7 SCAR=23.6
Pod documented
247 1 ▶ my $module = shift || (caller)[0];
Condition33%1/3
shift() || (caller)[0]
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || (caller)[0]
shift() (caller)[0]
Truth table33%1/3
A || B
A= shift()B= (caller)[0]
ABresult
1X1
011
000
248 1 ▶ my $filter = shift || '';
Condition50%1/2
shift() || ''
l!l
01
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
249
250 1 ▶ $module = ref $module
Branch: 1 branch
BranchTrueFalseTotal
if ref $module 0 1 1
251 if ref $module;
252
253 1 my $namespace = \%{$module . '::'};
254
255 1 my $linear_isa;
256
257 1 ▶ if ($] < 5.009_005) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.009005) { } 0 1 1
258 0 require Class::Easy::MRO;
259 0 $linear_isa = __get_linear_isa ($module);
260 } else {
261 1 require mro;
262 1 $linear_isa = mro::get_linear_isa ($module);
263 }
264
265 1 my $sub_by_type = list_local_subs_for ($module);
266 $sub_by_type->{inherited}->{$_} = [list_local_subs_for ($_)]
267 1 foreach grep {$_ ne $module} @$linear_isa;
268
269 wantarray
270 ? (
271 0 keys %{$sub_by_type->{method}},
272 0 keys %{$sub_by_type->{runtime}},
273 0 ▶ map {@{$sub_by_type->{inherited}->{$_}}} keys %{$sub_by_type->{inherited}})
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
274 : $sub_by_type;
275 }
276  
277 5 1;
278  
279 =head1 NAME
280  
281 Class::Easy - make class routine easy
282  
283 =head1 ABSTRACT
284  
285 This module is a functionality compilation of some good modules from CPAN.
286 Ideas are taken from Class::Data::Inheritable, Class::Accessor, Modern::Perl
287 and Moose at least.
288  
289 Instead of building monstrous alternatives to Moose or making thousand modules
290 for every function I need, I decide to write small and efficient libraries for
291 everyday use. Class::Easy::Base is a base component for classes.
292  
293 =head1 SYNOPSIS
294  
295 SYNOPSIS
296  
297 # automatic loading of strict, warnings and utf8, like common::sense
298 use Class::Easy::Import;
299 # or same as above + functions like 'has', 'try_to_use', 'timer' and 'logger'
300 use Class::Easy;
301
302 # try to load package IO::Easy, return 1 when success
303 try_to_use ('IO::Easy');
304
305 # try to load package IO::Easy, but search for package existence
306 # within %INC instead of symbolic table
307 try_to_use_inc ('IO::Easy');
308
309 # for current package
310 has "property_ro"; # make readonly object accessor
311 has "property_rw", is => 'rw'; # make readwrite object accessor
312
313 has global25 => 25; # make readonly static accessor with value 25
314 has "global", global => 1, is => 'rw'; # make readwrite static accessor
315  
316 # make subroutine in package main
317 make_accessor ('main', 'initialize', default => sub {
318 $::initialized = 1;
319 return "initialized!";
320 });
321
322 # see documentation for Class::Easy::Log
323
324 # string "[PID] [PACKAGE(STRING)] [DBG] something" logged
325 debug "something";
326  
327 # see documentation for Class::Easy::Timer
328  
329 my $t = timer ('long operation');
330 # … long operation
331  
332 my $time = $t->lap ('another long op');
333 # …
334  
335 $time = $t->end;
336 # $time contains time between last 'lap' or 'timer'
337 # and 'end' call
338  
339 $time = $t->total;
340 # now $time contains total time between timer init
341 # and end call
342  
343 =head1 FUNCTIONS
344  
345 =head2 has ($name [, is => 'ro' | 'rw'] [, default => $default], [, global => 1])
346  
347 create accessor named $name in current scope
348  
349 =cut
350  
351 =head2 make_accessor ($scope, $name)
352  
353 create accessor in selected scope
354  
355 =cut
356  
357 =head2 try_to_use, try_to_use_quiet
358  
359 tries to use specified package with printing error message to STDERR
360 or "_quiet" version.
361  
362 return true value in case of successful operation or existing non-package
363 references in symbol table. correctly works with virtual packages.
364  
365 takes package name or package name chunks, for example:
366  
367 try_to_use ('IO::Easy');
368 # or equivalent
369 try_to_use (qw(IO Easy));
370  
371 if you want to separate io errors from syntax errors you may want to
372 check $! variable;
373  
374 for example:
375  
376 use Errno qw(:POSIX);
377
378 if (!try_to_use ('IO::Easy')) {
379 die 'file not found for package IO::Easy'
380 if $!{ENOENT};
381 }
382  
383 =cut
384  
385 =head2 try_to_use_inc, try_to_use_inc_quiet
386  
387 similar to the try_to_use, but check for module presence in %INC
388 instead of symbol table lookup.
389  
390 =cut
391  
392 =head2 timer
393  
394 create new L<Class::Easy::Timer> object
395  
396 =cut
397  
398 =head2 get_coderef_info, stash_name, sub_name, sub_fullname
399  
400 retrieve real name for coderef. useful for anonymous or imported functions
401  
402 get_coderef_info (*{Class::Easy::timer}{CODE}); # ('Class::Easy', 'timer')
403 stash_name (*{Class::Easy::timer}{CODE}); # 'Class::Easy'
404 sub_name (*{Class::Easy::timer}{CODE}); # 'timer'
405 sub_fullname (*{Class::Easy::timer}{CODE}); # 'Class::Easy::timer'
406  
407 =cut
408  
409 =head2 list_all_subs_for, list_local_subs_for
410  
411 in scalar context return hashref with complete coderef info for class.
412 - key 'inherited' contains all inherited methods, separated by class name,
413 - key 'runtime' contains all code references in current package which point
414 to anonymous method,
415 - key 'method' contains all local methods,
416 - key 'imported' contains all imported subs, separated by class name
417  
418 {
419 'inherited' => {
420 'My::Circle' => [
421 'new',
422 'global_hash',
423 'global_hash_rw',
424 'new_default',
425 'global_hash_rw_default',
426 'dim_x',
427 'id',
428 'dim_y'
429 ]
430 },
431 'runtime' => {
432 'global_ro' => 1,
433 'global_one' => 1,
434 'global_one_defined' => 1,
435 'dim_z' => 1,
436 'accessor' => 1
437 },
438 'method' => {
439 'sub_z' => 1
440 },
441 'imported' => {
442 'Class::Easy' => {
443 'make_accessor' => 'make_accessor',
444 'try_to_use' => 'try_to_use',
445 'try_to_use_inc' => 'try_to_use_inc',
446 'try_to_use_quiet' => 'try_to_use_quiet',
447 'has' => 'has',
448 'timer' => 'timer',
449 'try_to_use_inc_quiet' => 'try_to_use_inc_quiet'
450 },
451 'Class::Easy::Log' => {
452 'critical' => 'critical',
453 'release_stderr' => 'release_stderr',
454 'catch_stderr' => 'catch_stderr',
455 'debug' => 'debug',
456 'debug_depth' => 'debug_depth',
457 'logger' => 'logger'
458 }
459 }
460 };
461  
462 'local' version of subroutine doesn't contains any inherited methods
463  
464  
465 =cut
466  
467  
468  
469 =head1 AUTHOR
470  
471 Ivan Baktsheev, C<< <apla at the-singlers.us> >>
472  
473 =head1 BUGS
474  
475 Please report any bugs or feature requests to my email address,
476 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Easy>.
477 I will be notified, and then you'll automatically be notified
478 of progress on your bug as I make changes.
479  
480 =head1 SUPPORT
481  
482  
483  
484 =head1 ACKNOWLEDGEMENTS
485  
486  
487  
488 =head1 COPYRIGHT & LICENSE
489  
490 Copyright 2008-2009 Ivan Baktsheev
491  
492 This program is free software; you can redistribute it and/or modify it
493 under the same terms as Perl itself.
494  
495 =cut
« blib/lib/Class/Easy/Log.pm ↑ Summary blib/lib/Class/Easy/Base.pm »