lib/Scalar/Classify.pm

filter: Statementstmt 93.1% 81 / 87 Branchbran 68.0% 34 / 50 Conditioncond 89.4% 17 / 19 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 85.1% 155 / 182 SCAR 36.1
CC
33
Cov
85%
classify_pair
36.8
classify
7.0
BEGIN
1.0
CRAP 37.0
« blib/lib/Scalar/Classify.pm ↑ Summary Build »
1 package Scalar::Classify;
2 # doom@kzsu.stanford.edu
3 # 30 Jun 2015
4  
5 =head1 NAME
6  
7 Scalar::Classify - get type and class information for scalars
8  
9 =head1 SYNOPSIS
10  
11 use Scalar::Classify qw( classify classify_pair );
12  
13 # determine the type (e.g. HASH for a hashref) and the object class (if any)
14 my ( $type, $class ) = classify( $some_scalar );
15  
16  
17 # warn if two args differ, supply default if one is undef
18 my $default_value =
19 classify_pair( $arg1, $arg2 );
20  
21 # Also get type and class; error out if two args differ
22 my ( $default_value, $type, $class ) =
23 classify_pair( $arg1, $arg2, { mismatch_policy => 'error' });
24  
25 # If a given ref was undef, replace it with a default value
26 classify_pair( $arg1, $arg2, { also_qualify => 1 });
27  
28 =head1 DESCRIPTION
29  
30 Scalar::Classify provides a routine named "classify" that can be used
31 to examine a given argument to determine it's type and class (if any).
32  
33 Here "type" means either the return from reftype (, or if it's a scalar,
34 a code indicating whether it's a string or a number, and "class"
35 it the object class, the way a reference has been blessed.
36  
37 This module also provides the routine "classify_pair", which
38 looks at a pair of variables intended to be of the same type, and
39 if at least one of them is defined, uses that to get an
40 appropriate default value for that type.
41  
42 =head2 MOTIVATION
43  
44 Perl contains a built-in "ref" function, and has some useful
45 routines in the standard Scalar::Util library ('ref',
46 'looks_like_number') which can be used to examine the type of an
47 argument. The classify routine provided here internally uses all
48 three of these, returning a two-values that describe the kind of
49 thing you're examining.
50  
51 The immediate goal was to provide support routines for the
52 L<Data::Math> project.
53  
54 =head2 EXPORT
55  
56 None by default. Optionally:
57  
58 =over
59  
60 =cut
61  
62 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 4 my $DEBUG = 1;
66 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 4 ▶use Scalar::Util qw( reftype looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 4 our (@ISA, @EXPORT_OK, %EXPORT_TAGS, @EXPORT);
71 BEGIN {
72 4 ▶ require Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 4 @ISA = qw(Exporter);
74 4 %EXPORT_TAGS = ( 'all' => [
75 qw(
76 classify
77 classify_pair
78 ) ] );
79 # The above allows use Scalar::Classify ':all';
80  
81 4 @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
82 4 @EXPORT = qw( );
83 }
84  
85 4 our $VERSION = '0.01';
86  
87 =item classify
88  
89 Example usage:
90  
91 my ( $type, $class ) = classify( $some_var );
92  
93 Returns two pieces of information, the underlying "type", and the
94 "class" (if this is a reference blessed into a class).
95  
96 The type is most often (but not limited to) one of the following:
97  
98 ARRAY
99 HASH
100 :NUMBER:
101 :STRING:
102  
103 Other possibilities are the other potential returns from L<ref>:
104  
105 CODE
106 GLOB
107 LVALUE
108 FORMAT
109 IO
110 VSTRING
111 Regexp
112  
113 Internally, this uses the built-in function L<ref> and the library
114 functions L<reftype> and L<looks_like_number> (from L<Scalar::Util>).
115 The type is the return from "reftype" (e.g "ARRAY", "HASH")
116 except that in the case of a simple scalar the type is a code to
117 indicate whether it seems to be a number (":NUMBER:") or a string
118 (":STRING:").
119  
120 Note: if the argument is undefined, the returned type is undef.
121  
122 =cut
123  
124 sub classify {
125 48 ▶ my $arg = shift;
Subroutine classify: called CC=7 SCAR=19.5
Pod documented
126  
127 # initialize $type to ref()
128 48 my $type = ref( $arg ); # '' if undef
129  
130 48 my $basetype = reftype( $arg ) ; # undef if undef
131  
132 48 my $class; # default undef
133 # it's a blessed ref when ref() not same as reftype()
134 48 ▶ if ( defined( $basetype ) && $type ne $basetype ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $basetype and $type ne $basetype) 14 34 48
Condition100%3/3
defined $basetype and $type ne $basetype
!ll&&!rl&&r
211314
MC/DC100%2/2
defined $basetype and $type ne $basetype
defined $basetype $type ne $basetype
Truth table100%3/3
A and B
A= defined $basetypeB= $type ne $basetype
ABresult
0X0
100
111
135 14 $class = $type;
136 14 $type = $basetype;
137 }
138  
139 48 ▶ if( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) { } 37 11 48
140 # if not reference, we're handling a scalar
141 37 ▶ if ( not( defined( $basetype ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $basetype) 10 27 37
142 10 ▶ if( looks_like_number( $arg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($arg)) { } 6 4 10
143 6 $type = ':NUMBER:';
144 } else {
145 4 $type = ':STRING:';
146 }
147 }
148 } else {
149 11 $type = undef; # more perlish than an empty string
150 }
151  
152 48 my @meta = ( $type, $class );
153 48 ▶ return wantarray ? @meta : \@meta;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 44 48
154 }
155  
156 =item classify_pair
157  
158 Examines a pair of arguments that are intended to be processed in
159 parallel and are expected to be of the same type:
160  
161 If they're both defined, it checks that their types match.
162 If at least one is defined, it generates a default of the
163 same type by using the L<classify> method. If both are
164 undef, this default is also undef.
165  
166 In scalar context, it returns just the default value.
167  
168 In list context, it returns the default plus the type and
169 the class (if it's a blessed reference).
170  
171 An options hashref is accepted as a third argument, with
172 allowed options:
173  
174 o mismatch_policy
175  
176 If argument types mismatch, the behavior is determined by
177 the mismatch_policy option, defaulting to 'warn'.
178 The other allowed values are 'error' or 'silent'.
179  
180 o also_qualify
181  
182 If the "also_qualify" option is set to a true value, then
183 the given arguments may be modified in place: if one is
184 undef, it will be assigned the determined default.
185  
186 Examples:
187  
188 my $default_value =
189 classify_pair( $arg1, $arg2, { mismatch_policy => 'error' });
190  
191 my ( $default_value, $type, $class ) =
192 classify_pair( $arg1, $arg2, { mismatch_policy => 'error' });
193  
194 classify_pair( $arg1, $arg2, { also_qualify => 1 });
195  
196 Note the slightly unusual polymorphic behavior: in scalar
197 context returns *just* the default_value, in list context,
198 returns up to three values, the default, the type and the class.
199  
200  
201 =cut
202  
203 sub classify_pair {
204 14 ▶ my $subname = ( caller(0) )[3];
Subroutine classify_pair: called CC=27 SCAR=36.0
Pod documented
205 14 my $opt = $_[2];
206  
207 14 ▶ my $policy = $opt->{ mismatch_policy } || 'warn';
Condition100%2/2
$opt->{'mismatch_policy'} || 'warn'
l!l
410
MC/DC100%1/1
$opt->{'mismatch_policy'} || 'warn'
$opt->{'mismatch_policy'}
Truth table100%2/2
A
A= $opt->{'mismatch_policy'}
Aresult
11
00
208 14 ▶ my $do_qualify = $opt->{ also_qualify } || 0;
Condition100%2/2
$opt->{'also_qualify'} || 0
l!l
113
MC/DC100%1/1
$opt->{'also_qualify'} || 0
$opt->{'also_qualify'}
Truth table100%2/2
A
A= $opt->{'also_qualify'}
Aresult
11
00
209  
210 14 my $meta1 = classify( $_[0] );
211 14 my $meta2 = classify( $_[1] );
212  
213 # handle mismatched types
214 14 ▶ if ( $policy ne 'silent' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy ne "silent") 14 0 14
215 4 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
216 14 ▶ if ( defined( $_[0] ) && defined( $_[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and defined $_[1]) 4 10 14
Condition100%3/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
734
MC/DC100%2/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table100%3/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
217 4 ▶ unless( $meta1->[0] eq $meta2->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta1->[0] eq $meta2->[0]) 3 1 4
218 3 ▶ croak "mismatched types: $meta1->[0] and $meta2->[0]" if $policy eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $policy eq "error" 3 0 3
219 0 ▶ carp "mismatched types: $meta1->[0] and $meta2->[0]" if $policy eq 'warn';
Branch: 1 branch
BranchTrueFalseTotal
if $policy eq "warn" 0 0 0
220 }
221 1 ▶ unless( $meta1->[1] eq $meta2->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta1->[1] eq $meta2->[1]) 1 0 1
222 1 ▶ croak "mismatched classes: $meta1->[1] and $meta2->[1]" if $policy eq 'error';
Branch: 1 branch
BranchTrueFalseTotal
if $policy eq "error" 1 0 1
223 0 ▶ carp "mismatched classes: $meta1->[1] and $meta2->[1]" if $policy eq 'warn';
Branch: 1 branch
BranchTrueFalseTotal
if $policy eq "warn" 0 0 0
224 }
225 }
226 }
227  
228 10 my ( $defval, $class, $type );
229 4 ▶ { no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
230 10 ▶ $type = $meta1->[0] || $meta2->[0];
Condition66%2/3
$meta1->[0] || $meta2->[0]
l!l&&r!l&&!r
370
MC/DC0%0/2
$meta1->[0] || $meta2->[0]
$meta1->[0] $meta2->[0]
Truth table66%2/3
A || B
A= $meta1->[0]B= $meta2->[0]
ABresult
1X1
011
000
231 }
232 10 ▶ unless( defined( $type ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 10 10
233 0 ▶ return wantarray ? ( undef, undef, undef ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
234 }
235  
236 10 ▶ if ( $type eq ':NUMBER:' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq ':NUMBER:') { } 3 7 10
elsif ($type eq ':STRING:') { } 1 6 7
237 3 $defval = 0;
238 } elsif ( $type eq ':STRING:' ) {
239 1 $defval = '';
240 } else {
241 4 ▶ { no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
242 6 ▶ $class = $meta1->[1] || $meta2->[1];
Condition100%3/3
$meta1->[1] || $meta2->[1]
l!l&&r!l&&!r
114
MC/DC100%2/2
$meta1->[1] || $meta2->[1]
$meta1->[1] $meta2->[1]
Truth table100%3/3
A || B
A= $meta1->[1]B= $meta2->[1]
ABresult
1X1
011
000
243 }
244 6 ▶ if ( $type eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 2 4 6
elsif ($type eq 'HASH') { } 4 0 4
elsif ($type eq 'SCALAR') { } 0 0 0
245 2 $defval = [];
246 } elsif ( $type eq 'HASH' ) {
247 4 $defval = {};
248 } elsif ( $type eq 'SCALAR' ) {
249 0 my $var;
250 0 $defval = \$var;
251 } else { # handle the useless cases: warn and get out of here
252 0 carp "$subname can't do anything useful with ref type $type";
253 }
254 }
255  
256 10 ▶ if( defined( $defval ) && defined( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defval and defined $class) 2 8 10
Condition66%2/3
defined $defval and defined $class
!ll&&!rl&&r
082
MC/DC50%1/2
defined $defval and defined $class
defined $defval defined $class
Truth table66%2/3
A and B
A= defined $defvalB= defined $class
ABresult
0X0
100
111
257 2 bless( $defval, $class );
258 }
259  
260 10 ▶ if( $do_qualify ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_qualify) 1 9 10
261 1 ▶ $_[0] = $defval unless defined( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 0 1
262 1 ▶ $_[1] = $defval unless defined( $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 1 1
263 }
264  
265 10 ▶ return wantarray ? ( $defval, $type, $class ) : $defval;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 1 10
266 }
267  
268  
269 4 1;
270  
271 =back
272  
273 =head1 SEE ALSO
274  
275 L<Params::Classify>
276  
277 This covers the argument checking case, where you want to verify
278 that something of the correct type was passed. The perl5-porters
279 are interested in adding core support for this module: it's fast
280 and likely to get faster.
281  
282 =head1 AUTHOR
283  
284 Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt>
285  
286 =head1 COPYRIGHT AND LICENSE
287  
288 Copyright (C) 2016 by Joseph Brenner
289  
290 This library is free software; you can redistribute it and/or modify
291 it under the same terms as Perl itself.
292  
293 See http://dev.perl.org/licenses/ for more information.
294  
295 =cut
« blib/lib/Scalar/Classify.pm ↑ Summary Build »