How to Check a Perl Email Address

How to Check a Perl Email Address thumbnail
Check email addresses with regular expressions in Perl.

Perl is a scripting language commonly used on Linux machines and servers to automate common administrative tasks, such as checking an email address given by a user to see if it is valid and fits the email address pattern. To do this, the key is the pattern matching syntax known as "Regular Expressions."

Instructions

    • 1

      Open a new text file. You can use any text editor you prefer, from Windows Notepad to a dedicated PERL Integrated Development Environment (IDE), such as EPIC or Padre. However, full scale word processors like Microsoft Word are inappropriate, since they do not, by default, output plain text.

    • 2

      Indicate that this is a perl file by pasting the following line at the very top of the text file:

      #!/usr/bin/perl

    • 3

      Store an email address in memory:

      $email="myemail\@email.com";

      Notice that the "@" symbol must be escaped (preceeded) with a backslash to let perl know it is part of the text and not code.

    • 4

      Compare it to the email test regular expression to see if it fits the pattern of a standard email:

      if ($email =~ m/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) {

      print "Yes.";

      } else {

      print "No.";

      }

      The "=~" operator tells Perl to compare a string to a regular expression, and the following regular expression dictates that the string must contain a number of letters and digits, followed by an @ sign, followed again by a series of numbers and digits and, finally, at least one extension between two and four characters in size.

    • 5

      Save your work with the file name "emailtest.pl." You can run the test by typing "perl emailtest.pl" at a terminal or command line.

Tips & Warnings

  • A detailed introduction to Regular Expression syntax is beyond the scope of this article. You can learn more about the syntax for regular expressions using the first resource.

  • In Windows, you can get to a command line by clicking "Start" and choosing "Run." Type "cmd." In Mac OS X, you can do it by pressing "Cmd-Spacebar" and typing "terminal."

Related Searches:

References

Resources

  • Photo Credit email image by Hao Wang from Fotolia.com

Comments

You May Also Like

Related Ads

Featured