Friday, 1 July 2011

Perl tutorial

1. Introduction

Perl was invented by Larry Wall. He called it Practical Extraction and Reporting Language (he also calls it Pathologically Eclectic Rubbish Lister). What started as an exercise in unifying multiple tools used to write scripts to make routine tasks of a system adminstrator evolved into a powerful scripting language with lots of followers. 
In all fairness, Perl (written always as Perl and not as PERL), is now treated as a generic programming language, though its early beginnings as a melting pot of multiple computing paradigms still make it possible to write undecipherable programs! We will try to get an introduction to Perl and its prowess as a text manipulation language without trying to write cryptic programs. 
According to Larry Wall, the parents of Perl are 
COMPUTER SCIENCE

LINGUISTICS                  Perl                   COMMON SENSE

                             ART
So, Perl is a computer language that helps to implement some common sense with help from the principles of computer science in an artistic way using common linguistic constructs. 

1.1 Short History

Like mentioned before, Perl was born as a tool to aid system administrators. In 1986, Larry Wall was asked to build a bi-coastal CM system in a very short time. That, he did and then his manager asked to produce reports from the system. Awk in those days did not have the capability to manage multiple input files and hence the new language was born. 
Primarily, the new language was aimed at getting things done quickly with data from and to multiple files. 

1.2 Evolution

From a quick hack by one system administrator, Perl has grown into a full-fledged language. It is being developed and enhanced continuously by hundreds of programmers around the world. One big step in earning recognition was the addition of regular expression engine. Now, the regular expression capabilities of Perl are so well known (especially since version 5.0), that it is being used in other languages like Python as Perl5 regex'es. 
The growth of Internet also complemented Perl. The initial attempt at providing dynamic content was through CGI (even now CGI is used extensively), and Perl's remarkable text handling features made it a quick fit. CGI programming is now synonymous with Perl programming. 
CPAN - Comprehensive Perl Archive Network, was set up to share Perl code. Perl supports modules and chances are that for 99% of the programming requirements, there is already a tested module in CPAN (for the remaining 1%, write modules and contribute to CPAN!). Using modules really mask the complexities of adhering to pre-defined standards and frees you to concentrate on your tasks - no point in re-inventing the wheel. Now, you have modules which handles graphics, CGI etc... 
You can also embed Perl code in your C/C++ programs. A very popular embedded Perl architecture is mod_perl for Apache web server. 
JAPH is a project to get Java and Perl working together. 

1.3 Relevance



Data manipulation

Perl can handle strings, dates, binary data, database connectivity, streams, sockets and many more. This ability to manipulate multiple data types help immensely in data conversion (and by the way, it is much faster than PL/SQL!). Perl also has provision for lists (or arrays) and for hashes (associative arrays). Perl also supports references, which are similar to the pointers in C. Lists, hashes and references together make it possible to define and manipulate powerful custom-defined data-types. 

Glue language

Perl does not differentiate between files and pipes. So, it makes it very easy to use Perl as a glue language. Suppose you have a sed script, the output of which is to be given to a Perl script. You can do this the UNIX way, 
sedscript | perlscript
or the perl way
perlscript
  open(FH,"sedscript|") or die "could not open sedscript\n"l
  ...
This really helps when people want to migrate from traditional UNIX tools like Awk, sed, grep etc... You can use these tools straightaway instead of worrying on how to do the same thing entirely in Perl. In this aspect, Perl is just like shell. However, we must consider other features of Perl, which shell simply cannot provide easily. 


CGI

CGI.pm. Period. Almost all CGI programs written today are using the CGI.pm module from CPAN. Even before this was written, people used to use Perl extensively for CGI programming. CGI.pm made the process streamlined and easy, even for beginners. The graphics library GD is used extensively in producing dynamic web charts. 

Quick coding

The ease with which Perl can be employed to write programs quickly cannot be overstressed. A disturbing fact about this is that such quick code can tend to be dirty and quickly get out of hand if you keep extending it! Most of the time, you must control your urges to over-extend short programs! But, as a prototyping tool, or as a fast reporting/text-processing tool, Perl is immensely helpful. 
Two very good tools worth mentioning in this context are s2p and a2p tools which come with the Perl distribution. s2p converts a sed script to Perl script and a2p converts from Awk scripts. These two help a lot in extending sed and awk scripts. 

Portability

Most of the Perl code will run without any change in Unix or Windows or Macintosh. Typical changes you might have to make include specifying file paths and use of low-level OS specific functions. 

1.4 Installation

Just go to http://www.perl.com and download the source or pre-compiled binaries. Installation typically includes extracting the binary and then changing your PATH variable to reflect where Perl executable resides. Even when you want to compile Perl from scratch, it is a simple job. 

1.5 Similarities to common languages/tools

Perl has a remarkable resemblance to the syntax of C, AWK, SED and SHELL. 

C

99% of code looks like C code. So, it is very easy for C programmers to switch to Perl. And believe me, the code as you go philosophy of Perl really makes C programmers happy - especially for small programs. All C functions that are available through standard libraries are available with little or no change at all in Perl. 

AWK & SED

The string processing strategy of Perl is very similar to that of Awk and sed, making it easy to migrate. 

Shell

Again, the commenting scheme, variable naming scheme etc of Perl look similar to that of Shell. Many shell utilities like grep, tr etc are available as functions within Perl.

0 comments:

Post a Comment

 
Related Posts Plugin for WordPress, Blogger...