File Coverage

blib/lib/Data/Sah/Coerce/perl/array/str_int_range_and_comma_sep.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 20 22 90.9


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::array::str_int_range_and_comma_sep;
2              
3             our $DATE = '2019-07-25'; # DATE
4             our $VERSION = '0.003'; # VERSION
5              
6 1     1   1802 use 5.010001;
  1         16  
7 1     1   5 use strict;
  1         2  
  1         15  
8 1     1   51 use warnings;
  1         2  
  1         164  
9              
10             sub meta {
11             +{
12 1     1 0 12 v => 4,
13             might_fail => 1,
14             prio => 60, # a bit lower than normal
15             };
16             }
17              
18             sub coerce {
19 1     1 0 14 my %args = @_;
20              
21 1         3 my $dt = $args{data_term};
22              
23 1         2 my $res = {};
24              
25 1         3 $res->{expr_match} = "!ref($dt)";
26              
27 1         4 $res->{expr_coerce} = join(
28             "",
29             "do { ",
30             "my \$res; my \$ary = []; ",
31             "for my \$elem (split /\\s*,\\s*/, $dt) { ",
32             " my (\$int1, \$int2) = \$elem =~ /\\A\\s*([+-]?\\d+)(?:\\s*(?:-|\\.\\.)\\s*([+-]?\\d+))?\\s*\\z/; ",
33             " if (!defined \$int1) { \$res = [\"Invalid elem '\$elem': must be INT or INT1-INT2\"]; last } ",
34             " if (defined \$int2) { ",
35             " if (\$int2 - \$int1 > 1_000_000) { \$res = [\"Elem '\$elem': Range too big\"]; last } ",
36             " push \@\$ary, \$int1+0 .. \$int2+0; ",
37             " } else { push \@\$ary, \$int1 } ",
38             "}", # for
39             "\$res ||= [undef, \$ary]; ",
40             "\$res }",
41             );
42              
43 1         4 $res;
44             }
45              
46             1;
47             # ABSTRACT: Coerce array of ints from comma-separated ints/int ranges
48              
49             __END__
50              
51             =pod
52              
53             =encoding UTF-8
54              
55             =head1 NAME
56              
57             Data::Sah::Coerce::perl::array::str_int_range_and_comma_sep - Coerce array of ints from comma-separated ints/int ranges
58              
59             =head1 VERSION
60              
61             This document describes version 0.003 of Data::Sah::Coerce::perl::array::str_int_range_and_comma_sep (from Perl distribution Data-Sah-CoerceRule-array-str_int_range_and_comma_sep), released on 2019-07-25.
62              
63             =head1 DESCRIPTION
64              
65             The rule is not enabled by default. You can enable it in a schema using e.g.:
66              
67             ["array*", of=>"int", "x.coerce_rules"=>["str_int_range_and_comma_sep"]]
68              
69             =for Pod::Coverage ^(meta|coerce)$
70              
71             =head1 HOMEPAGE
72              
73             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-CoerceRule-array-str_int_range_and_comma_sep>.
74              
75             =head1 SOURCE
76              
77             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-CoerceRule-array-str_int_range_and_comma_sep>.
78              
79             =head1 BUGS
80              
81             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-CoerceRule-array-str_int_range_and_comma_sep>
82              
83             When submitting a bug or request, please include a test-file or a
84             patch to an existing test-file that illustrates the bug or desired
85             feature.
86              
87             =head1 AUTHOR
88              
89             perlancar <perlancar@cpan.org>
90              
91             =head1 COPYRIGHT AND LICENSE
92              
93             This software is copyright (c) 2019, 2018 by perlancar@cpan.org.
94              
95             This is free software; you can redistribute it and/or modify it under
96             the same terms as the Perl 5 programming language system itself.
97              
98             =cut