File Coverage

blib/lib/Data/Sah/Coerce/perl/To_array/From_str/int_range.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::To_array::From_str::int_range;
2              
3             # AUTHOR
4             our $DATE = '2019-11-28'; # DATE
5             our $DIST = 'Data-Sah-CoerceBundle-To_array-From_str-int_range'; # DIST
6             our $VERSION = '0.006'; # VERSION
7              
8 1     1   1821 use 5.010001;
  1         3  
9 1     1   5 use strict;
  1         2  
  1         18  
10 1     1   4 use warnings;
  1         2  
  1         163  
11              
12             sub meta {
13             +{
14 1     1 0 12 v => 4,
15             summary => 'Coerce array of ints from string in the form of "INT1-INT2"',
16             might_fail => 1,
17             prio => 60, # a bit lower than normal
18             };
19             }
20              
21             sub coerce {
22 1     1 0 16 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) && $dt =~ /\\A\\s*[+-]?\\d+\\s*(?:-|\\.\\.)\\s*[+-]?\\d+\\s*\\z/";
29              
30 1         4 $res->{expr_coerce} = join(
31             "",
32             "do { ",
33             "my (\$int1, \$int2) = $dt =~ /\\A\\s*([+-]?\\d+)\\s*(?:-|\\.\\.)\\s*([+-]?\\d+)\\s*\\z/; ",
34             "if (\$int2 - \$int1 > 1_000_000) { ['Range too big'] } ",
35             "else { [undef, [\$int1+0 .. \$int2+0]] } ",
36             "}",
37             );
38              
39 1         3 $res;
40             }
41              
42             1;
43             # ABSTRACT: Coerce array of ints from string in the form of "INT1-INT2"
44              
45             __END__
46              
47             =pod
48              
49             =encoding UTF-8
50              
51             =head1 NAME
52              
53             Data::Sah::Coerce::perl::To_array::From_str::int_range - Coerce array of ints from string in the form of "INT1-INT2"
54              
55             =head1 VERSION
56              
57             This document describes version 0.006 of Data::Sah::Coerce::perl::To_array::From_str::int_range (from Perl distribution Data-Sah-CoerceBundle-To_array-From_str-int_range), released on 2019-11-28.
58              
59             =head1 SYNOPSIS
60              
61             To use in a Sah schema:
62              
63             ["array",{"x.perl.coerce_rules"=>["From_str::int_range"]}]
64              
65             =for Pod::Coverage ^(meta|coerce)$
66              
67             =head1 HOMEPAGE
68              
69             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-CoerceBundle-To_array-From_str-int_range>.
70              
71             =head1 SOURCE
72              
73             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-CoerceBundle-To_array-From_str-int_range>.
74              
75             =head1 BUGS
76              
77             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-CoerceBundle-To_array-From_str-int_range>
78              
79             When submitting a bug or request, please include a test-file or a
80             patch to an existing test-file that illustrates the bug or desired
81             feature.
82              
83             =head1 AUTHOR
84              
85             perlancar <perlancar@cpan.org>
86              
87             =head1 COPYRIGHT AND LICENSE
88              
89             This software is copyright (c) 2019, 2018 by perlancar@cpan.org.
90              
91             This is free software; you can redistribute it and/or modify it under
92             the same terms as the Perl 5 programming language system itself.
93              
94             =cut