File Coverage

blib/lib/Data/Sah/Coerce/perl/To_str/From_str/to_isbn.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 23 25 92.0


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::To_str::From_str::to_isbn;
2              
3             # AUTHOR
4             our $DATE = '2019-11-29'; # DATE
5             our $DIST = 'Sah-Schemas-ISBN'; # DIST
6             our $VERSION = '0.007'; # VERSION
7              
8 1     1   1711 use 5.010001;
  1         3  
9 1     1   4 use strict;
  1         2  
  1         17  
10 1     1   4 use warnings;
  1         2  
  1         186  
11              
12             sub meta {
13             +{
14 1     1 0 11 v => 4,
15             summary => 'Check and format ISBN 10 or ISBN 13 number from string',
16             might_fail => 1,
17             prio => 50,
18             };
19             }
20              
21             sub coerce {
22 1     1 0 14 my %args = @_;
23              
24 1         2 my $dt = $args{data_term};
25              
26 1         2 my $res = {};
27              
28 1         3 $res->{expr_match} = "!ref($dt)";
29 1         2 $res->{modules}{"Algorithm::CheckDigits"} = 0;
30 1         2 $res->{modules}{"Algorithm::CheckDigits::M10_004"} = 0;
31 1         1 $res->{modules}{"Algorithm::CheckDigits::M11_001"} = 0;
32 1         4 $res->{expr_coerce} = join(
33             "",
34             "do { my \$digits = $dt; \$digits =~ s/[^0-9Xx]//g; \$digits = uc \$digits; ",
35             "my \$res; ",
36             "{ ",
37             " if (length \$digits == 10) { unless (Algorithm::CheckDigits::CheckDigits('ISBN')->is_valid(\$digits)) { \$res = ['Invalid ISBN 10 checksum digit']; last } \$res = [undef, \$digits] } ",
38             " elsif (length \$digits == 13) { unless (Algorithm::CheckDigits::CheckDigits('ean') ->is_valid(\$digits)) { \$res = ['Invalid ISBN 13 checksum digit']; last } \$res = [undef, \$digits] } ",
39             " else { \$res = ['ISBN must be 10 or 13 digits']; last } ",
40             "} ",
41             "\$res }",
42             );
43              
44 1         4 $res;
45             }
46              
47             1;
48             # ABSTRACT: Check and format ISBN 10 or ISBN 13 number from string
49              
50             __END__
51              
52             =pod
53              
54             =encoding UTF-8
55              
56             =head1 NAME
57              
58             Data::Sah::Coerce::perl::To_str::From_str::to_isbn - Check and format ISBN 10 or ISBN 13 number from string
59              
60             =head1 VERSION
61              
62             This document describes version 0.007 of Data::Sah::Coerce::perl::To_str::From_str::to_isbn (from Perl distribution Sah-Schemas-ISBN), released on 2019-11-29.
63              
64             =head1 SYNOPSIS
65              
66             To use in a Sah schema:
67              
68             ["str",{"x.perl.coerce_rules"=>["From_str::to_isbn"]}]
69              
70             =for Pod::Coverage ^(meta|coerce)$
71              
72             =head1 HOMEPAGE
73              
74             Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-ISBN>.
75              
76             =head1 SOURCE
77              
78             Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-ISBN>.
79              
80             =head1 BUGS
81              
82             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-ISBN>
83              
84             When submitting a bug or request, please include a test-file or a
85             patch to an existing test-file that illustrates the bug or desired
86             feature.
87              
88             =head1 AUTHOR
89              
90             perlancar <perlancar@cpan.org>
91              
92             =head1 COPYRIGHT AND LICENSE
93              
94             This software is copyright (c) 2019, 2018 by perlancar@cpan.org.
95              
96             This is free software; you can redistribute it and/or modify it under
97             the same terms as the Perl 5 programming language system itself.
98              
99             =cut