File Coverage

blib/lib/Data/FormValidator/EmailValid.pm
Criterion Covered Total %
statement 24 24 100.0
branch 4 4 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 38 38 100.0


line stmt bran cond sub pod time code
1             package Data::FormValidator::EmailValid;
2              
3             ###############################################################################
4             # Required inclusions.
5             ###############################################################################
6 1     1   115804 use strict;
  1         17  
  1         35  
7 1     1   6 use warnings;
  1         1  
  1         28  
8 1     1   557 use Email::Valid;
  1         131385  
  1         115  
9              
10             ###############################################################################
11             # Make our methods exportable
12             ###############################################################################
13 1     1   18 use base qw( Exporter );
  1         8  
  1         444  
14             our @EXPORT_OK = qw(
15             FV_email_filter
16             FV_email
17             );
18              
19             ###############################################################################
20             # Version number.
21             ###############################################################################
22             our $VERSION = '0.09';
23              
24             ###############################################################################
25             # Subroutine: FV_email_filter(%options)
26             # Parameters: %options - Options for Email::Valid
27             ###############################################################################
28             # Filter method which cleans up the given value and returns valid e-mail
29             # addresses (or nothing, if the value isn't a valid e-mail address).
30             #
31             # "Valid" is deemed to mean "looks like an e-mail"; no other tests are done to
32             # ensure that a valid MX exists or that the address is actually deliverable.
33             #
34             # This filter method automatically converts all e-mail addresses to lower-case.
35             # This behaviour can be disabled by passing through an 'lc=>0' option.
36             #
37             # You may also pass through any additional 'Email::Valid' '%options' that you
38             # want to use; they're handed straight through to 'Email::Valid'.
39             ###############################################################################
40             sub FV_email_filter {
41 8     8 1 12072 my %options = @_;
42             # check if we should mangle the e-mail to all lower-case (default yes)
43 8         17 my $mangle_lc = 1;
44 8 100       25 $mangle_lc = delete $options{'lc'} if (exists $options{'lc'});
45             # return filter closure
46             return sub {
47 8     8   3316 my $email = shift;
48 8 100       56 return Email::Valid->address(
49             '-address' => $mangle_lc ? lc($email) : $email,
50             '-fudge' => 1,
51             '-mxcheck' => 0,
52             %options,
53             );
54 8         60 };
55             }
56              
57             ###############################################################################
58             # Subroutine: FV_email(%options)
59             # Parameters: %options - Options for Email::Valid
60             ###############################################################################
61             # Constraint method which checks to see if the value being constrained is a
62             # valid e-mail address or not. Returns true if the e-mail address is valid,
63             # false otherwise.
64             #
65             # This differs from the "email" constraint that comes with
66             # 'Data::FormValidator' in that we not only check to make sure that the e-mail
67             # looks valid, but also that a valid MX record exists for the address. No
68             # other checks are done to ensure that the address is actually deliverable,
69             # however.
70             #
71             # You can also pass through any additional 'Email::Valid' '%options' that you
72             # want to use; they're handed straight through to 'Email::Valid'.
73             ###############################################################################
74             sub FV_email {
75 2     2 1 153219 my %options = @_;
76             return sub {
77 2     2   1343 my $dfv = shift;
78             # get the value we're constraining
79 2         14 my $val = $dfv->get_current_constraint_value();
80             # check for valid e-mail address
81 2         31 my $rc = Email::Valid->address(
82             '-address' => $val,
83             '-mxcheck' => 1,
84             %options,
85             );
86 2         65807 return defined $rc;
87 2         44 };
88             }
89              
90             1;
91              
92             =for stopwords MX
93              
94             =head1 NAME
95              
96             Data::FormValidator::EmailValid - Data::FormValidator e-mail address constraint/filter
97              
98             =head1 SYNOPSIS
99              
100             use Data::FormValidator::EmailValid qw(FV_email_filter FV_email);
101              
102             my $results = Data::FormValidator->check(
103             {
104             'email' => 'Graham TerMarsch ',
105             },
106             {
107             'required' => [qw( email )],
108             'field_filters' => {
109             'email' => FV_email_filter(),
110             },
111             'constraint_methods' => {
112             'email' => FV_email(),
113             },
114             },
115             );
116              
117             =head1 DESCRIPTION
118              
119             C implements a constraint and filter for use
120             with C that do e-mail address validation/verification
121             using C.
122              
123             Although I generally find that I'm using the filter and constraint together,
124             they've been separated so that you could use just one or the other (e.g. you
125             may want to constrain on valid e-mail addresses without actually cleaning up or
126             filtering any of the data provided to you by the user).
127              
128             =head1 METHODS
129              
130             =over
131              
132             =item FV_email_filter(%options)
133              
134             Filter method which cleans up the given value and returns valid e-mail
135             addresses (or nothing, if the value isn't a valid e-mail address).
136              
137             "Valid" is deemed to mean "looks like an e-mail"; no other tests are done
138             to ensure that a valid MX exists or that the address is actually
139             deliverable.
140              
141             This filter method automatically converts all e-mail addresses to
142             lower-case. This behaviour can be disabled by passing through an
143             C0> option.
144              
145             You may also pass through any additional C C<%options> that
146             you want to use; they're handed straight through to C.
147              
148             =item FV_email(%options)
149              
150             Constraint method which checks to see if the value being constrained is a
151             valid e-mail address or not. Returns true if the e-mail address is valid,
152             false otherwise.
153              
154             This differs from the "email" constraint that comes with
155             C in that we not only check to make sure that the
156             e-mail looks valid, but also that a valid MX record exists for the address.
157             No other checks are done to ensure that the address is actually
158             deliverable, however.
159              
160             You can also pass through any additional C C<%options> that
161             you want to use; they're handed straight through to C.
162              
163             =back
164              
165             =head1 AUTHOR
166              
167             Graham TerMarsch (cpan@howlingfrog.com)
168              
169             =head1 COPYRIGHT
170              
171             Copyright (C) 2007, Graham TerMarsch. All Rights Reserved.
172              
173             This is free software; you can redistribute it and/or modify it under the same
174             license as Perl itself.
175              
176             =head1 SEE ALSO
177              
178             L,
179             L.
180              
181             =cut