File Coverage

blib/lib/Parse/Number/EN.pm
Criterion Covered Total %
statement 18 18 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Parse::Number::EN;
2              
3             # TODO: make it OO and customize thousand sep & decimal point
4              
5 1     1   441 use 5.010001;
  1         2  
  1         28  
6 1     1   3 use strict;
  1         1  
  1         20  
7 1     1   3 use warnings;
  1         1  
  1         38  
8              
9             our $VERSION = '0.05'; # VERSION
10              
11 1     1   447 use Exporter::Lite;
  1         512  
  1         3  
12             our @EXPORT_OK = qw($Pat parse_number_en);
13              
14             our %SPEC;
15              
16             #our $Pat = qr/(?:
17             # [+-]?
18             # (?:
19             # (?:\d{1,3}(?:[,]\d{3})+ | \d+) (?:[.]\d*)? | # english
20             # [.]\d+
21             # )
22             # (?:[Ee][+-]?\d+)?
23             # )/x;
24              
25             # non /x version
26             our $Pat = '(?:[+-]?(?:(?:\d{1,3}(?:[,]\d{3})+|\d+)(?:[.]\d*)?|[.]\d+)(?:[Ee][+-]?\d+)?)';
27              
28             $SPEC{parse_number_en} = {
29             v => 1.1,
30             summary => 'Parse number from English text',
31             description => <<'_',
32              
33             This function can parse number with thousand separators (e.g. 10,000).
34              
35             In the future percentage (e.g. 10.2%) and fractions (e.g. 1/3, 2 1/2) might also
36             be supported.
37              
38             _
39             args => {
40             text => {
41             summary => 'The input text that contains number',
42             schema => 'str*',
43             req => 1,
44             pos => 0,
45             },
46             },
47             result_naked => 1,
48             };
49             sub parse_number_en {
50 30     30 1 33758 my %args = @_;
51 30         35 my $text = $args{text};
52              
53 30 100       269 return undef unless $text =~ s/^\s*($Pat)//s;
54 24         56 my $n = $1;
55 24         29 $n =~ s/,//g;
56 24         84 $n+0;
57             }
58              
59             1;
60             # ABSTRACT: Pattern to match number
61              
62             __END__