Perl Programming Language

The overall structure of Perl derives broadly from C. Perl is procedural in nature, with variablesexpressionsassignment statementsbrace-delimited code blockscontrol structures, and subroutines.

Perl also takes features from shell programming. All variables are marked with leading sigils, which unambiguously identify the data type (scalar, array, hash, etc.) of the variable in context. Importantly, sigils allow variables to be interpolated directly into strings. Perl has many built-in functions which provide tools often used in shell programming (though many of these tools are implemented by programs external to the shell) like sorting, and calling on system facilities.

Perl takes lists from Lisp, associative arrays (hashes) from AWK, and regular expressions from sed. These simplify and facilitate many parsing, text handling, and data management tasks.

In Perl 5, features were added that support complex data structuresfirst-class functions (i.e., closures as values), and an object-oriented programming model. These include references, packages, class-based method dispatch, and lexically scoped variables, along with compiler directives (for example, the strict pragma). A major additional feature introduced with Perl 5 was the ability to package code as reusable modules. Larry Wall later stated that “The whole intent of Perl 5’s module system was to encourage the growth of Perl culture rather than the Perl core.”[14]

All versions of Perl do automatic data typing and memory management. The interpreter knows the type and storage requirements of every data object in the program; it allocates and frees storage for them as necessary using reference counting (so it cannot deallocate circular data structures without manual intervention). Legal type conversions—for example, conversions from number to string—are done automatically at run time; illegal type conversions are fatal errors.

<http://en.wikipedia.org/wiki/Perl>

I have found it to be one of the easiest languages to be learned while programming, it is vastly known to many programmers and also its is the ideal starting program. Many times I have found this to be a Quiet enjoyable language.

Here’s An example of a Phone Data Progam, found in most Cellular phones…

$ans = 0;
while($ans != 6)
{
   printf("Menu: Add(1)  Edit(2)  Search(3)  Delete(4)  PrintList(5)  Quit(6)
");
   chomp($ans = <>);

   if($ans == 1) # Add
    {
      printf("Enter First Name: ");      chomp($FirstName = <>);
      printf("Enter Last Name: ");       chomp($LastName = <>);
      printf("Enter phone 1: ");           chomp($phone1 =<>);
      printf("Enter Phone 2: ");          chomp($phone2 = <>);

      $contact{uc($FirstName._.$LastName)} = "$phone1|$phone2";
      printf("$FirstName $LastName contact is added\n");
    }

    if($ans == 2) # Edit
    {
      printf("Enter First Name: ");      chomp($FirstName = <>);
      printf("Enter Last Name: ");       chomp($LastName = <>);
      printf("Enter New phone 1: ");     chomp($phone1 =<>);
      printf("Enter New Phone 2: ");     chomp($phone2 = <>);

      $contact{uc($FirstName._.$LastName)} = "$phone1|$phone2";
      printf("$FirstName $LastName Phones are updated\n");
    }

   if($ans == 3) # search
    {
     printf("Enter First Name: ");      chomp($FirstName = <>);
     printf("Enter Last Name: ");       chomp($LastName = <>);
     $key = uc($FirstName._.$LastName);
        $value = $contact{$key};
        if (defined($value))
         {
           @field = split(/_/, $key);
           $firstname= $field[0];
           $lastname = $field[1];
           @field = split(/\|/,$value);
           $phone1 = $field[0];
           $phone2 = $field[1];
           printf("Phone 1 is: %s\nPhone 2 is: %s\n",$phone1,$phone2);
         }
    }

   if($ans == 4)  # delete
    {
      printf("Enter First Name: ");      chomp($FirstName = <>);
      printf("Enter Last Name: ");       chomp($LastName = <>);
      delete $contact{uc($FirstName._.$LastName)};
      printf("$FirstName $LastName contact is deleted\n");
    }

   if($ans == 5)   # print list
    {
     foreach $key(sort keys %contact) {
      $value = $contact{$key};
      @field = split(/_/, $key);
      $FirstName= $field[0];
      $LastName = $field[1];
      @field = split(/\|/,$value);
      $phone1 = $field[0];
      $phone2 = $field[1];
      printf("FirstName: %s\nLastName: %s\nPhone1: %s\nPhone2:
%s\n\n",$FirstName,$LastName,$phone1,$phone2);
     }
    }
}

dbmclose(%contact);

I could explain what these symbols and signs mean, but That would take a long time, I suggest the perl website; perl.com

Leave any comments relating to this section of perl, and i’ll answer them as soon as possible. :]

 

Published on July 21, 2008 at 1:47 am Leave a Comment

The URI to TrackBack this entry is: http://nikhilscorner.wordpress.com/perl-programming-language/trackback/

RSS feed for comments on this post.

Leave a Comment