/
Regexes.pm
95 lines (58 loc) · 1.73 KB
/
Regexes.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package Brick::Regexes;
use strict;
use base qw(Exporter);
use vars qw($VERSION);
$VERSION = '0.903';
package Brick::Bucket;
use strict;
use Carp qw(croak);
=encoding utf8
=head1 NAME
Brick - This is the description
=head1 SYNOPSIS
use Brick::Constraints;
=head1 DESCRIPTION
See C<Brick::Constraints> for the general discussion of constraint
creation.
=head2 Utilities
=over 4
=item _matches_regex( HASHREF )
Create a code ref to apply a regular expression to the named field.
field - the field to apply the regular expression to
regex - a reference to a regular expression object ( qr// )
=cut
sub _matches_regex {
my( $bucket, $setup ) = @_;
my @caller = $bucket->__caller_chain_as_list();
unless( eval { $setup->{regex}->isa( ref qr// ) } ) {
croak( "Argument to $caller[0]{'sub'} must be a regular expression object" );
}
$bucket->add_to_bucket ( {
name => $setup->{name} || $caller[0]{'sub'},
description => ( $setup->{description} || "Match a regular expression" ),
fields => [ $setup->{field} ],
code => sub {
die {
message => "[$_[0]->{ $setup->{field} }] did not match the pattern",
failed_field => $setup->{field},
failed_value => $_[0]->{ $setup->{field} },
handler => $caller[0]{'sub'},
} unless $_[0]->{ $setup->{field} } =~ m/$setup->{regex}/;
},
} );
}
=back
=head1 TO DO
Regex::Common support
=head1 SEE ALSO
TBA
=head1 SOURCE AVAILABILITY
This source is in Github:
https://github.com/briandfoy/brick
=head1 AUTHOR
brian d foy, C<< <briandfoy@pobox.com> >>
=head1 COPYRIGHT
Copyright © 2007-2024, brian d foy <briandfoy@pobox.com>. All rights reserved.
You may redistribute this under the terms of the Artistic License 2.0.
=cut
1;