File Coverage

blib/lib/Data/Validate/Currency.pm
Criterion Covered Total %
statement 16 16 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 5 5 100.0
pod 1 1 100.0
total 26 27 96.3


line stmt bran cond sub pod time code
1             package Data::Validate::Currency;
2              
3 1     1   63208 use strict;
  1         2  
  1         29  
4 1     1   4 use warnings;
  1         1  
  1         22  
5              
6 1     1   4 use Carp qw(croak);
  1         1  
  1         36  
7 1     1   3 use Exporter;
  1         2  
  1         150  
8              
9             our @ISA = qw(Exporter);
10             our @EXPORT_OK = qw( is_currency );
11              
12             our $VERSION = '0.1.0'; # VERSION 0.1.0
13             #ABSTRACT: Module to validate if data is valid currency
14              
15             =pod
16              
17             =encoding utf8
18              
19             =head1 NAME
20              
21             Data::Validate::Currency - Module to validate if string is a valid currency format
22              
23             =head1 SYNOPSIS
24              
25             use strict;
26             use warnings;
27              
28             use Data::Validate::Currency qw( is_currency );
29              
30             my $currency_string = '$1,000.00';
31             my $currency_validation = is_currency($currency_string);
32              
33             print "Is a valid currency string\n" if $currency_validation;
34              
35             =head1 DESCRIPTION
36              
37             Module that takes a string and returns a true or false value if the string
38             is a valid currency. Supports the comma as thousands-separator format,
39             2 and 3 digit decimals. Dollar sign is optional.
40              
41             =head1 METHODS
42              
43             =head2 is_currency
44              
45             use strict;
46             use warnings;
47             use Data::Validate::Currency qw(is_currency);
48              
49             my $currency_string = '$1,000.00';
50             my $currency_validation = is_currency($currency_string);
51              
52             Returns 1 if it is valid currency format, 0 if it is not. Dollar sign optional.
53              
54             =head1 Author
55              
56             Daniel Culver, C<< perlsufi@cpan.org >>
57              
58             =head1 ACKNOWLEDGEMENTS
59              
60             Eris Caffee, C<< eris-caffee@eldalin.com >>
61             - A majority of the credit goes to Eris for the final regex.
62              
63             Robert Stone, C<< drzigman@cpan.org >>
64             - Robert initially started the regex.
65              
66             HostGator
67              
68             =head1 COPYRIGHT
69              
70             This module is free software; you can redistribute it and/or modify it
71             under the same terms as Perl itself.
72              
73             =cut
74              
75             sub is_currency {
76              
77 15   66 15 1 15223 my $string = shift || croak "is_currency requires a currency string";
78              
79 14 100       84 if ( $string =~
80             /^\${0,1}\d{1,3}(\,\d{3})*\.\d{2,3}$|^\${0,1}\d+\.\d{2,3}$/ ) {
81 13         27 return 1;
82             }
83             else {
84 1         3 return 0;
85             }
86             }
87              
88             1;