lib/Caller/Easy.pm

filter: Statementstmt 97.6% 83 / 85 Branchbran 67.3% 31 / 46 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 13 / 13 Podpod 50.0% 1 / 2 totaltotal 85.5% 142 / 166 CC 28 SCAR 29.2
CC
28
Cov
86%
BUILD
30.4
__ANON__
6.0
BEGIN
1.0
CRAP 18.6
« blib/lib/Caller/Easy.pm ↑ Summary Build »
1 package Caller::Easy;
2 7 ▶use 5.014004;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = "0.02";
8  
9 7 ▶use overload '""' => sub{ $_[0]->package() }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 has 'depth' => ( is => 'ro', isa => 'Maybe[Num]' );
14 7 has 'package' => ( is => 'ro', writer => '_set_package', isa => 'Str' );
15 7 has 'filename' => ( is => 'ro', writer => '_set_filename', isa => 'Str' );
16 7 has 'line' => ( is => 'ro', writer => '_set_line', isa => 'Num' );
17 7 has 'subroutine' => ( is => 'ro', writer => '_set_subroutine', isa => 'Str' );
18 7 has 'hasargs' => ( is => 'ro', writer => '_set_hasargs', isa => 'Bool' );
19 7 has 'wantarray' => ( is => 'ro', writer => '_set_wantarray', isa => 'Bool' );
20 7 has 'evaltext' => ( is => 'ro', writer => '_set_evaltext', isa => 'Str' );
21 7 has 'is_require' => ( is => 'ro', writer => '_set_is_require', isa => 'Bool' );
22 7 has 'hints' => ( is => 'ro', writer => '_set_hints', isa => 'Num' );
23 7 has 'bitmask' => ( is => 'ro', writer => '_set_bitmask', isa => 'Str' );
24 7 has 'hinthash' => ( is => 'ro', writer => '_set_hinthash', isa => 'Maybe[HashRef]' );
25 7 has 'args' => ( is => 'ro', writer => '_set_args', isa => 'Maybe[ArrayRef]' );
26  
27 around BUILDARGS => sub {
28 21 ▶ my $orig = shift;
Subroutine __ANON__: called CC=6 SCAR=17.9
29 21 my $class = shift;
30  
31 21 ▶ if( @_ == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1) { } 13 8 21
elsif (@_ > 2) { } 1 7 8
elsif (@_ == 2 and not exists $_{'depth'}) { } 1 6 7
32 13 ▶ return $class->$orig( depth => $_[0] ) if $_[0] =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^\d+$/u 11 2 13
33 2 croak 'Unvalid depth was assigned';
34 }elsif( @_ > 2 ) {
35 1 croak 'Too many arguments for caller';
36 ▶ }elsif( @_ == 2 and not exists $_{depth} ) {
Condition66%2/3
@_ == 2 and not exists $_{"depth"}
!ll&&!rl&&r
601
MC/DC50%1/2
@_ == 2 and not exists $_{"depth"}
@_ == 2 not exists $_{"depth"}
Truth table66%2/3
A and not(B)
A= @_ == 2B= not exists $_{"depth"}
ABresult
0X0
100
111
37 1 croak 'Unvalid arguments for caller';
38 }else{
39 6 return $class->$orig(@_);
40 }
41 7 };
42  
43 sub BUILD {
44 17 ▶ my $self = shift;
Subroutine BUILD: called CC=23 SCAR=34.1
Pod undocumented
45 17 my $depth = $self->depth();
46  
47 my (
48 17 $package, $filename, $line,
49 $subroutine, $hasargs, $wantarray, $evaltext,
50 $is_require, $hints, $bitmask, $hinthash
51 );
52  
53  
54 17 ▶ if( defined $depth and $depth =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $depth and $depth =~ /^\d+$/u) { } 11 6 17
Condition66%2/3
defined $depth and $depth =~ /^\d+$/u
!ll&&!rl&&r
6011
MC/DC50%1/2
defined $depth and $depth =~ /^\d+$/u
defined $depth $depth =~ /^\d+$/u
Truth table66%2/3
A and B
A= defined $depthB= $depth =~ /^\d+$/u
ABresult
0X0
100
111
55 11 package DB {
56 11 our @args;
57 11 my $i = 1;
58 11 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$package =~ /^Test::/u || $package =~ /^Caller::Easy/u and do { do { () = caller $i++ } } 11 11 22
Condition66%2/3
$package =~ /^Test::/u or $package =~ /^Caller::Easy/u
l!l&&r!l&&!r
01111
MC/DC50%1/2
$package =~ /^Test::/u or $package =~ /^Caller::Easy/u
$package =~ /^Test::/u $package =~ /^Caller::Easy/u
Truth table66%2/3
A or B
A= $package =~ /^Test::/uB= $package =~ /^Caller::Easy/u
ABresult
1X1
011
000
59 22 ( $package, $filename, $line ) = CORE::caller($i++);
60 } while( $package =~ /^Test::/ or $package =~ /^Caller::Easy/ );
61  
62 (
63 undef, undef, undef,
64 11 $subroutine, $hasargs, $wantarray, $evaltext,
65 $is_require, $hints, $bitmask, $hinthash
66 ) = CORE::caller( $depth + $i++ );
67 }
68 }else{
69 6 my $i = 1;
70 6 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$package =~ /^Test::/u || $package =~ /^Caller::Easy/u and do { do { () = CORE::caller $i++ } } 9 6 15
Condition100%3/3
$package =~ /^Test::/u or $package =~ /^Caller::Easy/u
l!l&&r!l&&!r
456
MC/DC100%2/2
$package =~ /^Test::/u or $package =~ /^Caller::Easy/u
$package =~ /^Test::/u $package =~ /^Caller::Easy/u
Truth table100%3/3
A or B
A= $package =~ /^Test::/uB= $package =~ /^Caller::Easy/u
ABresult
1X1
011
000
71 15 ( $package, $filename, $line ) = CORE::caller($i++);
72 } while( $package =~ /^Test::/ or $package =~ /^Caller::Easy/ );
73  
74 6 ▶ $self->_set_package($package) if $package;
Branch: 1 branch
BranchTrueFalseTotal
if $package 6 0 6
75 6 ▶ $self->_set_filename($filename) if $filename;
Branch: 1 branch
BranchTrueFalseTotal
if $filename 6 0 6
76 6 ▶ $self->_set_line($line) if $line;
Branch: 1 branch
BranchTrueFalseTotal
if $line 6 0 6
77  
78 6 ▶ return $self unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::wantarray 6 0 6
79 0 return ( $package, $filename, $line );
80 }
81  
82 11 ▶ $self->_set_package($package) if $package;
Branch: 1 branch
BranchTrueFalseTotal
if $package 11 0 11
83 11 ▶ $self->_set_filename($filename) if $filename;
Branch: 1 branch
BranchTrueFalseTotal
if $filename 11 0 11
84 11 ▶ $self->_set_line($line) if $line;
Branch: 1 branch
BranchTrueFalseTotal
if $line 11 0 11
85 11 ▶ $self->_set_subroutine($subroutine) if $subroutine;
Branch: 1 branch
BranchTrueFalseTotal
if $subroutine 11 0 11
86 11 ▶ $self->_set_hasargs($hasargs) if defined $hasargs;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hasargs 11 0 11
87 11 ▶ $self->_set_wantarray($wantarray) if defined $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wantarray 3 8 11
88 11 ▶ $self->_set_evaltext($evaltext) if $evaltext;
Branch: 1 branch
BranchTrueFalseTotal
if $evaltext 0 11 11
89 11 ▶ $self->_set_is_require($is_require) if defined $is_require;
Branch: 1 branch
BranchTrueFalseTotal
if defined $is_require 0 11 11
90 11 ▶ $self->_set_hints($hints) if $hints;
Branch: 1 branch
BranchTrueFalseTotal
if $hints 11 0 11
91 11 ▶ $self->_set_bitmask($bitmask) if $bitmask;
Branch: 1 branch
BranchTrueFalseTotal
if $bitmask 11 0 11
92 11 ▶ $self->_set_hinthash($hinthash) if $hinthash;
Branch: 1 branch
BranchTrueFalseTotal
if $hinthash 0 11 11
93 11 $self->_set_args(\@DB::args);
94  
95 11 ▶ return $self unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::wantarray 11 0 11
96 return (
97 0 $package, $filename, $line,
98 $subroutine, $hasargs, $wantarray, $evaltext,
99 $is_require, $hints, $bitmask, $hinthash
100 );
101 }
102  
103 7 __PACKAGE__->meta->make_immutable;
104 7 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 sub caller {
107 20 ▶ __PACKAGE__->new(@_);
Subroutine caller: called CC=1 SCAR=0.0
Pod documented
108 }
109  
110 sub import {
111 7 ▶ my $packagename = CORE::caller;
Subroutine import: called CC=1 SCAR=0.0
112 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
113 7 *{"$packagename\::caller"} = \&caller;
114 }
115  
116 7 1;
117 __END__
118  
119 =encoding utf-8
120  
121 =head1 NAME
122  
123 Caller::Easy - less stress to remind returned list from CORE::caller()
124  
125 =head1 SYNOPSIS
126  
127 use Caller::Easy; # Module name is temporal
128  
129 # the way up to now
130 sub foo {
131 my $subname = (caller(0))[3];
132 }
133  
134 # with OO
135 sub foo {
136 my $subname = Caller::Easy->new(0)->subroutine();
137 }
138  
139 # like a function imported
140 sub foo {
141 my $subname = caller(0)->subroutine();
142 }
143  
144 All the above will return 'main::foo'
145  
146 Now you can choise the way you much prefer
147  
148 =head1 DESCRIPTION
149  
150 Caller::Easy is the easiest way for using functions of C<CORE::caller()>
151  
152 it produces the easier way to get some info from C<caller()> with no having to care about namespace.
153  
154 =head2 Constructor and initialization
155  
156 =head3 new()
157  
158 You can set no argument then it returns the object reference in scalar context.
159  
160 In list context, you can get just only ( $package, $filename, $line ).
161  
162 if you set depth(level) like C<new(1)>, you can get more info from caller
163 ( $package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext,
164 $is_require, $hints, $bitmask, $hinthash )
165 directly with same term for C<CORE::caller()>
166  
167 To be strictly, you can set C<depth> parameter like C<new( depth =E<gt> 1 )>
168 but we can forget it, just set the natural number you want to set.
169  
170 =head2 Methods (All of them is read-only)
171  
172 =head3 caller()
173  
174 It is implemented to be alias of C<new()> but it doesn't matter.
175  
176 this method is imported to your packages automatically when you C<use Caller::Easy;>
177  
178 So you can use much freely this method like if there is no module imported.
179  
180 =head3 package()
181  
182 You can get package name instead of C<(caller(n))[0]>
183  
184 =head3 filename()
185  
186 You can get file name instead of C<(caller(n))[1]>
187  
188 =head3 line()
189  
190 You can get the line called instead of C<(caller(n))[2]>
191  
192 =head3 subroutine()
193  
194 You can get the name of subroutine instead of C<(caller(n))[3]>
195  
196 =head3 hasargs(), wantarray(), evaltext(), is_require(), hints(), bitmask(), hinthash()
197  
198 Please read L<CORE::caller|http://perldoc.perl.org/functions/caller.html>
199  
200 B<Don't ask me>
201  
202 =head3 args()
203  
204 You can get the arguments of targeted subroutine instead of C<@DB::args>
205  
206 This method is the B<unique> point of this module.
207  
208 =head3 depth()
209  
210 You can get what you set.
211  
212 =head1 TODO
213  
214 =over
215  
216 =item using Moose is a bottle-neck
217  
218 I made this module in a few days with Moose because it's the easiest way.
219 It will be too heavy for some environments.
220  
221 To abolish Moose is a TODO if this module will be popular.
222  
223 =item rewite the tests
224  
225 I don't know well about L<CORE::caller|http://perldoc.perl.org/functions/caller.html>!
226  
227 Why I have written this module is
228 Just only I can't remember what I wanna get with something number from caller()
229 without some reference.
230  
231 So some of tests may not be appropriate.
232  
233 =item rename the module
234  
235 I have to find the name that deserve it.
236  
237 =item rewite the POD
238  
239 I know well that my English is awful.
240  
241 =back
242  
243 =head1 SEE ALSO
244  
245 =over
246  
247 =item L<CORE::caller|http://perldoc.perl.org/functions/caller.html>
248  
249 If you are confused with this module, Please read this carefully.
250  
251 =item L<Perl6::Caller|http://search.cpan.org/~ovid/Perl6-Caller/lib/Perl6/Caller.pm>
252  
253 One of better implements for using something like this module.
254  
255 The reason why I reinvent the wheel is that this module has no github repository.
256  
257 =item L<Safe::Caller|https://github.com/stsc/Safe-Caller>
258  
259 The newest implement for using something like this module.
260  
261 It has github repository but usage is limited.
262  
263 =back
264  
265 =head1 LICENSE
266  
267 Copyright (C) Yuki Yoshida.
268  
269 This library is free software; you can redistribute it and/or modify
270 it under the same terms as Perl itself.
271  
272 =head1 AUTHOR
273  
274 Yuki Yoshida E<lt>worthmine@gmail.comE<gt>
275  
276 =cut
« blib/lib/Caller/Easy.pm ↑ Summary Build »