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

Is there any other better way of doing it?
have I missed any thing?
Please Suggest!
I have build up a code which validate email id,
Note, "ITX" is default company and any email id
having @itx should have .ca at the end.
use strict; use warnings; my @id = (' @itx.org ', 'tart@itx.com', 't_art@hotmail.com'); my @validid; foreach(@id) { my $valid = validate($_); if($valid) { push(@validid, $valid); } else { print "Invalid id $_\n"; exit; } } print "Valid IDs: @validid\n"; sub validate { my $id = shift; $id =~ s/\s+$//; $id =~ s/^\s+//; if($id =~ /^@|^\./) { return; } elsif($id =~ /^.*\@itx\.com/i) { if($id !~ /ca$/) { return "$id.ca" if($id =~ /com$/); } else { return "$id"; } } elsif($id =~ /^.*\@\w+\.(?:com|org|net)/) { return "$id"; } return; }
Cheers!
Update
Thanks! jwkrahn suggestion implemented!

Replies are listed 'Best First'.
Re: Validate email id
by jwkrahn (Abbot) on Jul 15, 2010 at 00:27 UTC
    $id =~ s/\s+$//g; $id =~ s/^\s+//g;

    The /g option means "global" but the anchors mean only at the beginning or end so remove the /g option.

    elsif($id =~ /^.*\@\w+\.com|org|net/)

    That says to match the pattern ^.*\@\w+\.com OR the pattern org OR the pattern net.    You need parentheses to group the alternation correctly:

    elsif($id =~ /^.*\@\w+\.(?:com|org|net)/)
Re: Validate email id
by roboticus (Chancellor) on Jul 15, 2010 at 11:37 UTC

    tart:

    When you create test cases, be sure to look at all the statements, so you can test them. For example, add this test case: 'org@foo.com.bar'. You may find other errors if you add more test cases for your other tests and requirements.

    ...roboticus

    Update: Added "and requirements"