blib/lib/GetNum.pm

filter: Statementstmt 100% 18 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 23 / 23 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package GetNum;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Inline 'C' => <<'EOC';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #include <stdlib.h>
7  
8 SV* coerce_num_string(SV* sv, int iType) {
9 if(SvOK(sv) == 0 || SvPOK(sv) == 0) {
10 return( NULL );
11 }
12  
13 STRLEN len;
14 errno = 0;
15 char *endptr;
16 char *str = SvPV(sv,len);
17  
18 double iSV = strtod( str, &endptr );
19 if(errno != 0 || endptr == str) {
20 return( NULL );
21 }
22
23 if(iType == 1) {
24 return( newSViv( iSV ) );
25 }
26 else {
27 return( newSVnv( iSV ) );
28 }
29 }
30  
31 SV* get_int(SV* sv) {
32 if(SvIOK(sv) != 0 || SvNOK(sv) != 0) {
33 return( newSViv( SvIV(sv) ) );
34 }
35 else if(SvPOK(sv) != 0) {
36 return( coerce_num_string(sv,1) );
37 }
38 else {
39 return( NULL );
40 }
41 }
42  
43 SV* get_float(SV* sv) {
44 if(SvNOK(sv) != 0 || SvIOK(sv) != 0) {
45 return( newSVnv( SvNV(sv) ) );
46 }
47 else if(SvPOK(sv) != 0) {
48 return( coerce_num_string(sv,2) );
49 }
50 else {
51 return( NULL );
52 }
53 }
54  
55 int is_int(SV* sv) {
56 return SvIOK(sv);
57 }
58  
59 int is_float(SV* sv) {
60 return SvNOK(sv);
61 }
62 EOC
63  
64 2 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 our @EXPORT = qw(get_int is_int is_float get_float);
66  
67 2 ▶use version; our $VERSION = qv(1.0.2);
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 2 1;
70  
71 =pod
72  
73 =head1 NAME
74  
75 GetNum - coerce scalars into numbers or return undef
76  
77 =head1 SYNOPSIS
78  
79 use GetNum;
80  
81 my $i = get_int('foo'); # returns undef
82 my $i = get_int('123'); # returns 123
83  
84 =head1 DESCRIPTION
85  
86 This module can be used to force scalar to be coerced into numeric types
87 in a situation when the effect of C<int()> to turn non-numerics into
88 0 (zero) is not desirable.
89  
90 This module is handy when programming a serialization strategy for
91 a Perl object into JSON where datatypes affect the output.
92  
93 =head1 EXPORTED SUBROUTINES
94  
95 All subroutines are exported by default.
96  
97 =over
98  
99 =item * B<iget_int> - coerce a scalar into an integer or return undef
100  
101 =item * B<is_int> - returns true if a scalar is an integer
102  
103 =item * B<get_float> - coerce a scalar into a float or return undef
104  
105 =item * B<is_float> - returns true if a scalar is a float
106  
107 =back
108  
109 =head1 SEE ALSO
110  
111 The discussion on SV (Perl scalar variables) manipulation functions
112 in the documentation for the perlapi: L<http://perldoc.perl.org/perlapi.html#SV-Manipulation-Functions>
113  
114 =head1 DEPENDENCIES
115  
116 =over
117  
118 =item * Inline::C
119  
120 =back
121  
122 =head1 Revision History
123  
124 =over
125  
126 =item v1.0.0 - Original package submitted September 2014
127  
128 =item v1.0.1 - Added POD
129  
130 =item v1.0.2 - Added README file
131  
132 =back
133  
134 =head1 Author
135  
136 =over
137  
138 =item Aaron Dallas
139  
140 =item adallas@cpan.org
141  
142 =back
143  
144 =cut
↑ Summary