blib/lib/Class/CGI/Email/Valid.pm

filter: Statementstmt 100% 23 / 23 Branchbran 100% 2 / 2 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 89.1% 33 / 37 CC 4 SCAR 9.2
CC
4
Cov
93%
handle
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.5
↑ Summary
1 package Class::CGI::Email::Valid;
2  
3 2 ▶use base 'Class::CGI::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Class::CGI::Email::Valid - Validate email from forms
11  
12 =head1 VERSION
13  
14 Version 0.01
15  
16 =cut
17  
18 2 our $VERSION = '0.01';
19  
20 =head1 SYNOPSIS
21  
22 use Class::CGI handlers => {
23 email => 'Class::CGI::Email::Valid',
24 };
25 my $cgi = Class::CGI->new;
26 my $email = $cgi->param('email');
27  
28 if ( my %error_for = $cgi->errors ) {
29 if ( $error_for{email} ) {
30 ...
31 }
32 }
33  
34 =head1 DESCRIPTION
35  
36 Normally we fetch email from forms, run it through C<Email::Valid> or
37 something similar, untaint it, if necessary, and save it somewhere. This
38 class handles the email validation via C<Email::Valid> and optionally handles
39 untainting.
40  
41 Unlike other C<Class::CGI> handlers, this handler returns the email address
42 unchanged; the C<param()> method does not return an object. If the email
43 address failed to validate, the error message will be in the error hash
44 returned by the C<errors> method. As usual, the error key will be the name of
45 the param used.
46  
47 =head1 Basic usage
48  
49 use Class::CGI handlers => {
50 email_address => 'Class::CGI::Email::Valid',
51 };
52 my $cgi = Class::CGI->new;
53 my $email = $cgi->param('email_address');
54  
55 Any parameter name may be validated as an email address. If the value of the
56 parameter does not appear to be a valid email address,
57 B<the value entered will still be returned>! This makes it easy to create
58 "sticky" forms.
59  
60 =head2 Untainting
61  
62 This handler does not provide any untainting facilities. It merely checks
63 that the email address entered validated with C<Email::Valid>. This is
64 because email addresses often get used in the shell and it is very difficult
65 to ensure that the full range of email addresses allowed are safe for such
66 use. It is the responsibility of the programmer to ensure that a valid email
67 address is safe for such use.
68  
69 =head2 Overridding the error message
70  
71 If you prefer, you can override the default error message by setting the
72 "error" parameter in the C<Class::CGI::args()> hash.
73
74 use Class::CGI handlers => {
75 email => 'Class::CGI::Email::Valid',
76 };
77 my $cgi = Class::CGI->new;
78 $cgi->args( email => { error => "You gave me a bad email address, dummy!" } );
79
80 my $email = $cgi->param('email');
81  
82 =cut
83  
84 2 ▶sub handle {
Subroutine handle: called CC=4 SCAR=14.0
Pod documented
85 2 my $self = shift;
86 2 my $cgi = $self->cgi;
87 2 my $param = $self->param;
88 2 my $email = $cgi->raw_param($param);
89 2 ▶ my $args = $cgi->args($param) || {};
Condition50%1/2
$cgi->args($param) || {}
l!l
02
MC/DC0%0/1
$cgi->args($param) || {}
$cgi->args($param)
Truth table50%1/2
A
A= $cgi->args($param)
Aresult
11
00
90 my $error = $args->{error}
91 2 ▶ || "The email address did not appear to be valid";
Condition50%1/2
$args->{'error'} || 'The email address did not appear to be valid'
l!l
02
MC/DC0%0/1
$args->{'error'} || 'The email address did not appear to be valid'
$args->{'error'}
Truth table50%1/2
A
A= $args->{'error'}
Aresult
11
00
92 2 ▶ unless ( Email::Valid->address($email) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Email::Valid"->address($email)) 1 1 2
93 1 $cgi->add_error( $param, $error );
94 }
95 2 return $email;
96 }
97  
98 =head1 AUTHOR
99  
100 Curtis "Ovid" Poe, C<< <ovid@cpan.org> >>
101  
102 =head1 BUGS
103  
104 Please report any bugs or feature requests to
105 C<bug-class-cgi-email-valid@rt.cpan.org>, or through the web interface at
106 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-CGI-Email-Valid>.
107 I will be notified, and then you'll automatically be notified of progress on
108 your bug as I make changes.
109  
110 =head1 ACKNOWLEDGEMENTS
111  
112 =head1 COPYRIGHT & LICENSE
113  
114 Copyright 2006 Curtis "Ovid" Poe, all rights reserved.
115  
116 This program is free software; you can redistribute it and/or modify it
117 under the same terms as Perl itself.
118  
119 =cut
120  
121 2 1; # End of Class::CGI::Email::Valid
↑ Summary