line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Transpose::Validator::Subrefs; |
2
|
12
|
|
|
12
|
|
17616
|
use strict; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
406
|
|
3
|
12
|
|
|
12
|
|
58
|
use warnings; |
|
12
|
|
|
|
|
21
|
|
|
12
|
|
|
|
|
347
|
|
4
|
12
|
|
|
12
|
|
6900
|
use Moo; |
|
12
|
|
|
|
|
160569
|
|
|
12
|
|
|
|
|
69
|
|
5
|
|
|
|
|
|
|
extends 'Data::Transpose::Validator::Base'; |
6
|
12
|
|
|
12
|
|
25971
|
use MooX::Types::MooseLike::Base qw(:all); |
|
12
|
|
|
|
|
79841
|
|
|
12
|
|
|
|
|
6693
|
|
7
|
12
|
|
|
12
|
|
9586
|
use namespace::clean; |
|
12
|
|
|
|
|
120908
|
|
|
12
|
|
|
|
|
176
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Data::Transpose::Validator::Subrefs Validator using custom subroutines |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub custom_sub { |
14
|
|
|
|
|
|
|
my $field = shift; |
15
|
|
|
|
|
|
|
return $field |
16
|
|
|
|
|
|
|
if $field =~ m/\w/; |
17
|
|
|
|
|
|
|
return (undef, "Not a \\w"); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $vcr = Data::Transpose::Validator::Subrefs->new( \&custom_sub ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
ok($vcr->is_valid("H!"), "Hi! is valid"); |
23
|
|
|
|
|
|
|
ok(!$vcr->is_valid("!"), "! is not"); |
24
|
|
|
|
|
|
|
is($vcr->error, "Not a \\w", "error displayed correctly"); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head2 new(\&subroutine) |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
The constructor accepts only one argument, a reference to a |
32
|
|
|
|
|
|
|
subroutine. The class will provide the variable to validate as the |
33
|
|
|
|
|
|
|
first and only argument. The subroutine is expected to return a |
34
|
|
|
|
|
|
|
true value on success, or a false value on failure. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
To set a custom error, the subroutine in case of error should return 2 |
37
|
|
|
|
|
|
|
elements, where the first should be undefined (see the example above). |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
has call => (is => 'rw', isa => CodeRef, required => 1); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 call |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Accessor to the subroutine |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head2 is_valid($what) |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
The call to the validator. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub is_valid { |
56
|
8
|
|
|
8
|
1
|
107
|
my ($self, $arg) = @_; |
57
|
8
|
|
|
|
|
39
|
$self->reset_errors; |
58
|
8
|
|
|
|
|
442
|
my ($result, $error) = $self->call->($arg); |
59
|
8
|
100
|
|
|
|
1936
|
if ($error) { |
60
|
2
|
|
|
|
|
14
|
$self->error($error); |
61
|
2
|
|
|
|
|
14
|
return undef; |
62
|
|
|
|
|
|
|
} else { |
63
|
6
|
|
|
|
|
30
|
return $result; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub BUILDARGS { |
68
|
|
|
|
|
|
|
# straight from the manual |
69
|
7
|
|
|
7
|
0
|
5771
|
my ($class, @args) = @_; |
70
|
7
|
100
|
|
|
|
44
|
unshift @args, 'call' if @args % 2 == 1; |
71
|
7
|
|
|
|
|
153
|
return { @args }; |
72
|
|
|
|
|
|
|
}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
1; # the last famous words |
76
|
|
|
|
|
|
|
|