peschkaj has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a CGI script which will, eventually, end up sending an email to several people. I'm attempting to make use of the e-mail validation script that is on page 219 of CGI Programming with Perl.

When I try to run this script from the web, I receive the following error: 'Use of uninitialized value in substitution (s///) at mail.cgi line 218.'

Lines 217 and 218 are:

my $addr_to_check = shift; $addr_to_check =~ s/("(?:[^"\\]|\\.)*"|[^\t "]*)[ \t]*/$1/g;

I'm not too good at regexes (in fact, I'm terrible at them) and any help is appreciated.

Thanks in advance,
Jeremiah

Replies are listed 'Best First'.
Re: CGI + regex help
by dragonchild (Archbishop) on Oct 09, 2003 at 22:24 UTC
    Two things. First off, why aren't you using Email::Valid

    Secondly, I just searched CGI's documentation, but I don't think you can retrieve a parameter using $q->param(-name => 'next_email');. I think that syntax is only for setting a parameter's value. I could be wrong, but over 100 mentions of param() in the perldoc and not one example or mention of retrieval using the named syntax. I would try using the direct syntax, instead.

    Also, You're mentioning having issues with the CGI as object. Make sure that you're not mixing procedural and OO interfaces. That's a really easy way to move to a rubber room.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      It works:

      # perl -MCGI -le'print "works" if CGI->new()->param(-name=>"a")' a=1 works #

      Also, good point about Email::Valid.

      antirice    
      The first rule of Perl club is - use Perl
      The
      ith rule of Perl club is - follow rule i - 1 for i > 1

Re: CGI + regex help
by antirice (Priest) on Oct 09, 2003 at 22:10 UTC

    This means that $addr_to_check is undef. Perhaps we could offer more help if you were to provide more code such as what's around these two lines. Is it a subroutine? If so, under what conditions is it called?

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      The code in question lives inside of a subroutine. I have included the full subroutine code as well as how it is called.

      my $q = new CGI; my $email = validate_email_address( $q->param( -name=>"email" ) ); sub validate_email_address { my $addr_to_check = shift; $addr_to_check =~ s/("(?:[^"\\]|\\.)*"|[^\t "]*)[ \t]*/$1/g; my $esc = '\\\\'; my $space = '\040'; my $ctrl = '\000-\037'; my $dot = '\.'; my $nonASCII = '\x80-\xff'; my $CRlist = '\012\015'; my $letter = 'a-zA-Z'; my $digit = '\d'; my $atom_char = qq{ [^$space<>\@,;:".\\[\\]$esc$ctrl$nonASCII] } +; my $atom = qq{ $atom_char+ }; my $byte = qq{ (?: 1?$digit?$digit | 2[0-4]$digit | 25[0-5] ) }; my $qtext = qq{ [^$esc$nonASCII$CRlist"] }; my $quoted_pair = qq{ $esc [^$nonASCII] }; my $quoted_str = qq{ " (?: $qtext | $quoted_pair )* " }; my $word = qq{ (?: $atom | $quoted_str ) }; my $ip_address = qq{ \\[ $byte (?: $dot $byte ){3} \\] }; my $sub_domain = qq{ [$letter$digit] [$letter$digit-]{0,61} [$letter$digit]}; my $top_level = qq{ (?: $atom_char ){2,4} }; my $domain_name = qq{ (?: $sub_domain $dot )+ $top_level }; my $domain = qq{ (?: $domain_name | $ip_address ) }; my $local_part = qq{ $word (?: $dot $word )* }; my $address = qq{ $local_part \@ $domain }; return $addr_to_check =~ /^$address$/ox ? $addr_to_check : ""; }

      I'm also having problems with passing the $q object reference about to other subroutines, but I think that's something for another SoPW...

        Ok. We know that '', 0, and undef are all invalid e-mail addresses so just alter the validate_email_address sub as such:

        sub validate_email_address { my $addr_to_check = shift or return "";

        Hope this helps.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1