• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Perl

Perl (Practical Extraction and Reporting Language) has grown from a cult following in the early 1990s to a massive hit today. Originally designed to make working with text data simple, Perl has been expanded by developers to handle tasks such as image manipulation and client/server activities. Because of its ease of use and capability to work with ambiguous user input, Perl is an extremely popular Web development language. For example, assume that you want to extract a phone number from an input string. A user might enter 555-5654, 5552231, 421-5552313, and so on. It is up to the application to find the area code, local exchange, and identifier numbers. In Perl, this is simple:

#!/usr/bin/perl
print "Please enter a phone number:";
$phone=<STDIN>;
$phone=~s/[^\d]//g;
$phone=~s/^1//;
if (length($phone)==7) {
    $phone=~/(\d{ 3,3} )(\d{ 4,4} )/;
    $area="???"; $prefix=$1; $number=$2;
} elsif (length($phone)==10) {
    $phone=~/(\d{ 3,3} )(\d{ 3,3} )(\d{ 4,4} )/;
    $area=$1; $prefix=$2; $number=$3;
} else {  print "Invalid number!"; exit; }
print "($area) $prefix-$number\n";


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint