File Coverage

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