Number/Tolerant/Type.pm

filter: Statementstmt 96.5% 28 / 29 Branchbran 87.5% 7 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 89.0% 49 / 55 CC 6 SCAR 11.4
CC
6
Cov
90%
normalize_number
6.3
BEGIN
1.0
BEGIN
1.0
CRAP 3.1
« Number/Tolerant/Type/to.pm ↑ Summary Number/Tolerant/Type/plus_or_minus.pm »
1 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Number::Tolerant::Type 1.711; # TRIAL
4 # ABSTRACT: a type of tolerance
5  
6 25 ▶use parent qw(Number::Tolerant);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Math::BigRat;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =head1 SYNOPSIS
11 #pod
12 #pod =cut
13  
14 #pod =head1 METHODS
15 #pod
16 #pod =head2 valid_args
17 #pod
18 #pod my @args = $type_class->valid_args(@_);
19 #pod
20 #pod If the arguments to C<valid_args> are valid arguments for this type of
21 #pod tolerance, this method returns their canonical form, suitable for passing to
22 #pod C<L</construct>>. Otherwise this method returns false.
23 #pod
24 #pod =head2 construct
25 #pod
26 #pod my $object_guts = $type_class->construct(@args);
27 #pod
28 #pod This method is passed the output of the C<L</valid_args>> method, and should
29 #pod return a hashref that will become the guts of a new tolerance.
30 #pod
31 #pod =head2 parse
32 #pod
33 #pod my $tolerance = $type_class->parse($string);
34 #pod
35 #pod This method returns a new, fully constructed tolerance from the given string
36 #pod if the given string can be parsed into a tolerance of this type.
37 #pod
38 #pod =head2 number_re
39 #pod
40 #pod my $number_re = $type_class->number_re;
41 #pod
42 #pod This method returns the regular expression (as a C<qx> construct) used to match
43 #pod number in parsed strings.
44 #pod
45 #pod =head2 normalize_number
46 #pod
47 #pod my $number = $type_class->normalize_number($input);
48 #pod
49 #pod This method will decide whether the given input is a valid number for use with
50 #pod Number::Tolerant and return it in a canonicalized form. Math::BigInt objects
51 #pod are returned intact. Strings holding numbers are also returned intact.
52 #pod Strings that appears to be fractions are converted to Math::BigRat objects.
53 #pod
54 #pod Anything else is considered invalid, and the method will return false.
55 #pod
56 #pod =cut
57  
58 25 my $number;
59 25 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
60 25 $number = qr{
61 (?:
62 (?:[+-]?)
63 (?=[0-9]|\.[0-9])
64 [0-9]*
65 (?:\.[0-9]*)?
66 (?:[Ee](?:[+-]?[0-9]+))?
67 )
68 |
69 (?:
70 [0-9]+ / [1-9][0-9]*
71 )
72 }x;
73 }
74  
75 212 ▶sub number_re { return $number; }
Subroutine number_re: called CC=1 SCAR=0.0
Pod documented
76  
77 497 ▶sub normalize_number {
Subroutine normalize_number: called CC=6 SCAR=18.5
Pod documented
78 497 my ($self, $input) = @_;
79  
80 497 ▶ return if not defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 82 415 497
81  
82 415 ▶ if ($input =~ qr{\A$number\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ qr/\A$number\z/) 350 65 415
83 350 ▶ return $input =~ m{/} ? Math::BigRat->new($input) : $input;
Branch: 1 branch
BranchTrueFalseTotal
$input =~ m[/] ? : 2 348 350
84 # my $class = $input =~ m{/} ? 'Math::BigRat' : 'Math::BigRat';
85 # return $class->new($input);
86 }
87  
88 65 local $@;
89 0 ▶ return $input if ref $input and eval { $input->isa('Math::BigInt') };
Branch: 1 branch
BranchTrueFalseTotal
if ref $input and eval { do { $input->isa("Math::BigInt") } } 0 65 65
Condition33%1/3
ref $input and eval { do { $input->isa("Math::BigInt") } }
!ll&&!rl&&r
6500
MC/DC0%0/2
ref $input and eval { do { $input->isa("Math::BigInt") } }
ref $input eval { do { $input->isa("Math::BigInt") } }
Truth table33%1/3
A and B
A= ref $inputB= eval { do { $input->isa("Math::BigInt") } }
ABresult
0X0
100
111
90  
91 65 return;
92 }
93  
94 #pod =head2 variable_re
95 #pod
96 #pod my $variable_re = $type_class->variable_re;
97 #pod
98 #pod This method returns the regular expression (as a C<qr> construct) used to match
99 #pod the variable in parsed strings.
100 #pod
101 #pod When parsing "4 <= x <= 10" this regular expression is used to match the letter
102 #pod "x."
103 #pod
104 #pod =cut
105  
106 my $X;
107 25 ▶BEGIN { $X = qr/(?:\s*x\s*)/; }
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 129 ▶sub variable_re { return $X; }
Subroutine variable_re: called CC=1 SCAR=0.0
Pod documented
110  
111 25 1;
112  
113 __END__
« Number/Tolerant/Type/to.pm ↑ Summary Number/Tolerant/Type/plus_or_minus.pm »