Opts.pm

filter: Statementstmt 82.0% 64 / 78 Branchbran 71.4% 10 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 77.2% 17 / 22 Podpod 27.2% 3 / 11 totaltotal 73.3% 99 / 135 CC 12 SCAR 12.0
CC
12
Cov
79%
m4i_opts
9.2
new
4.0
take_hash_maybe
4.0
CRAP 3.3
« Base/JSON.pm ↑ Summary blib/lib/MOP4Import/Util/CommentedJson.pm »
1 package MOP4Import::Opts;
2 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 22 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use overload '""' => 'as_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 22 ▶use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 BEGIN {
10 22 ▶ print STDERR "Using (file '" . __FILE__ . "')\n" if DEBUG and DEBUG >= 2
Subroutine BEGIN: called CC=1 SCAR=0.0
11 }
12  
13 22 ▶use MOP4Import::Util qw/globref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 use fields
16 (
17 # caller() of import in usual case.
18 22 'callpack'
19  
20 # Where to export. Always defined.
21 , 'destpkg'
22  
23 # What to define. Optional.
24 , 'objpkg'
25  
26 # What to inherit. Optional.
27 , 'basepkg'
28  
29 # Used in MOP4Import::Types::Extend and MOP4Import::Declare::Type
30 , 'extending'
31  
32 # original caller info. This may be empty for faked m4i_opts()
33 , 'caller'
34  
35 # closures which are called at the each end of MOP4Import handling.
36 , 'delayed_tasks'
37  
38 # default value for json_type. 'string' if not specified
39 , 'default_json_type'
40  
41 # Cache to store and keep heavy computation results between pragmas.
42 , 'stash'
43  
44 , qw/filename line/
45 22 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 22 ▶use MOP4Import::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 #========================================
50  
51 sub Opts () {__PACKAGE__}
52  
53 sub new {
54 423 ▶ my ($pack, %opts) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
55  
56 423 my Opts $opts = fields::new($pack);
57  
58 423 ▶ if (my $caller = delete $opts{caller}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $caller = delete $opts{"caller"}) 423 0 423
59 423 $opts->{caller} = $caller;
60 ($opts->{callpack}, $opts->{filename}, $opts->{line})
61 423 ▶ = ref $caller ? @$caller : ($caller, '', '');
Branch: 1 branch
BranchTrueFalseTotal
ref $caller ? : 329 94 423
62 }
63  
64 423 $opts->{delayed_tasks} = [];
65  
66 423 $opts->{$_} = $opts{$_} for keys %opts;
67  
68 423 $opts->{objpkg} = $opts->{destpkg} = $opts->{callpack};
69  
70 423 $opts;
71 }
72  
73 sub take_hash_maybe {
74 69 ▶ (my Opts $opts, my $list) = @_;
Subroutine take_hash_maybe: called CC=4 SCAR=13.9
Pod undocumented
75  
76 69 ▶ return $opts unless @$list and ref $list->[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless @$list and ref $list->[0] eq "HASH" 65 4 69
Condition100%3/3
@$list and ref $list->[0] eq "HASH"
!ll&&!rl&&r
1644
MC/DC100%2/2
@$list and ref $list->[0] eq "HASH"
@$list ref $list->[0] eq "HASH"
Truth table100%3/3
A and B
A= @$listB= ref $list->[0] eq "HASH"
ABresult
0X0
100
111
77  
78 4 my $o = shift @$list;
79  
80 4 $opts->{$_} = $o->{$_} for keys %$o;
81  
82 4 $opts;
83 }
84  
85 # Should I use Clone::clone?
86 sub clone {
87 49 ▶ (my Opts $old) = @_;
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
88 49 my Opts $new = fields::new(ref $old);
89 49 %$new = %$old;
90 49 $new;
91 }
92  
93 0 ▶sub with_destpkg { my Opts $new = clone($_[0]); $new->{destpkg} = $_[1]; $new }
Subroutine with_destpkg: not called CC=1 SCAR=6.9
Pod undocumented
94 49 ▶sub with_objpkg { my Opts $new = clone($_[0]); $new->{objpkg} = $_[1]; $new }
Subroutine with_objpkg: called CC=1 SCAR=0.0
Pod undocumented
95 0 ▶sub with_basepkg { my Opts $new = clone($_[0]); $new->{basepkg} = $_[1]; $new }
Subroutine with_basepkg: not called CC=1 SCAR=6.9
Pod undocumented
96  
97 # XXX: Not extensible. but how?
98 sub m4i_opts {
99 5034 ▶ my ($arg) = @_;
Subroutine m4i_opts: called CC=6 SCAR=22.1
Pod documented
100 5034 ▶ if (not ref $arg) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $arg) { } 94 4940 5034
elsif (UNIVERSAL::isa($arg, 'MOP4Import::Opts')) { } 4633 307 4940
elsif (ref $arg eq 'ARRAY') { } 307 0 307
elsif (ref $arg eq 'HASH' and m4i_opts_alike($arg)) { } 0 0 0
101 # Fake Opts from string.
102 94 Opts->new(caller => $arg);
103  
104 } elsif (UNIVERSAL::isa($arg, Opts)) {
105 # Pass through.
106 4633 $arg
107  
108 } elsif (ref $arg eq 'ARRAY') {
109 # Shorthand of MOP4Import::Opts->new(caller => [caller]).
110 307 Opts->new(caller => $arg);
111  
112 ▶ } elsif (ref $arg eq 'HASH' and m4i_opts_alike($arg)) {
Condition0%0/3
ref $arg eq "HASH" and m4i_opts_alike($arg)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $arg eq "HASH" and m4i_opts_alike($arg)
ref $arg eq "HASH" m4i_opts_alike($arg)
Truth table0%0/3
A and B
A= ref $arg eq "HASH"B= m4i_opts_alike($arg)
ABresult
0X0
100
111
113 # pass-thru when m4i_opts-compatible plain HASH is given.
114 0 $arg;
115  
116 } else {
117 0 Carp::croak("Unknown argument! ". MOP4Import::Util::terse_dump($arg));
118 }
119 }
120  
121 sub m4i_opts_alike {
122 0 ▶ my ($arg) = @_;
Subroutine m4i_opts_alike: not called CC=1 SCAR=6.9
Pod undocumented
123 0 not grep {not exists $arg->{$_}}
124 qw(caller callpack filename line)
125 }
126  
127 sub m4i_args {
128 4727 ▶ ($_[0], m4i_opts($_[1]), @_[2..$#_]);
Subroutine m4i_args: called CC=1 SCAR=0.0
Pod documented
129 }
130  
131 sub m4i_fake_opts {
132 22 ▶ my ($fakedCallpack) = @_;
Subroutine m4i_fake_opts: called CC=1 SCAR=0.0
Pod undocumented
133 22 (undef, my (@callerTail)) = caller;
134 22 Opts->new(caller => [$fakedCallpack, @callerTail]);
135 }
136  
137 sub as_string {
138 0 ▶ (my Opts $opts) = @_;
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
139 0 $opts->{callpack};
140 }
141  
142 # Provide field getters.
143 22 foreach my $field (keys our %FIELDS) {
144 0 ▶ *{globref(Opts, $field)} = sub {shift->$field()};
Subroutine __ANON__: not called CC=1 SCAR=0.4
145 }
146  
147 22 our @EXPORT = qw/
148 Opts
149 m4i_args
150 /;
151 22 our @EXPORT_OK = (@EXPORT, MOP4Import::Util::function_names
152 (matching => qr/^(with_|m4i_)/));
153  
154 22 1;
155  
156 =head1 NAME
157  
158 MOP4Import::Opts - Object to encapsulate caller() record
159  
160 =head1 SYNOPSIS
161  
162 # To import the type 'Opts' and m4i api functions.
163 use MOP4Import::Opts;
164  
165 # To create an instance of MOP4Import::Opts.
166 sub import {
167 ...
168 my Opts $opts = m4i_opts([caller]);
169 ...
170 }
171  
172 # To extract MOP4Import::Opts safely from pragma args.
173 sub declare_foo {
174 (my $myPack, my Opts $opts, my (@args)) = m4i_args(@_);
175 ...
176 }
177  
178 =head1 DESCRIPTION
179  
180 This hash object encapsulates L<caller()|perlfunc/caller> info
181 and other parameters for L<MOP4Import::Declare> family.
182  
183 =head1 ATTRIBUTES
184  
185 =over 4
186  
187 =item callpack
188  
189 L<scalar caller()|perlfunc/caller> of import in usual case.
190  
191 =item destpkg
192  
193 Where to export. Always defined.
194  
195 =item objpkg
196  
197 What to define. Optional.
198  
199 =item basepkg
200  
201 What to inherit. Optional.
202  
203 =item extending
204  
205 Used in MOP4Import::Types::Extend and MOP4Import::Declare::Type
206  
207 =item caller
208  
209 Original L<caller()|perlfunc/caller> info.
210 This may be empty for faked m4i_opts().
211  
212 =item filename
213  
214 =item line
215  
216 =back
217  
218 =head1 FUNCTIONS
219  
220 =head2 m4i_args
221  
222 This function converts C<$_[1]> by L<m4i_opts> and returns whole C<@_>.
223  
224 (my $myPack, my Opts $opts, my (@args)) = m4i_args(@_);
225  
226 =head2 m4i_opts
227  
228 my Opts $opts = m4i_opts([caller]);
229  
230 my Opts $opts = m4i_opts(scalar caller); # string is ok too.
231  
232 =head1 METHODS
233  
234 =head2 as_string
235  
236 $opts->as_string;
237  
238 "$opts"; # Same as above.
239  
240 =head1 AUTHOR
241  
242 Kobayashi, Hiroaki E<lt>hkoba@cpan.orgE<gt>
243  
244 =head1 LICENSE
245  
246 This library is free software; you can redistribute it and/or modify
247 it under the same terms as Perl itself.
248  
249 =cut
250  
« Base/JSON.pm ↑ Summary blib/lib/MOP4Import/Util/CommentedJson.pm »