Getting Started with Perl

Perl

Practical Extraction and Report Language, commonly known as Perl, is an open source programming language that operates in the server side and is used for developing web scripts and processing data through CGI (Common Gateway Interface). Perl was designed by Larry Wall and was released in the market in 1987.

In a broad sense we can divide programming language in two main categories -- Compiled Language and Interpreted Language. Programming languages, which act like compilers and create machine codes from the source codes, are called Compiled Language (For example C, C++ etc.). Interpreted Languages, on the other hand, are the programming languages that save the code in the same format that you entered; the program is reduced to machine instructions at runtime. BASIC, Shell etc. are the examples of some common interpreted languages. Apart from these two categories there is a third type of programming language, which carries the features of both Compiled Language and Interpreted Language - common example of which is Perl. However, Perl borrows features from C, shell scripting, awk, sed, Lisp and many other programming languages.

Initially people started using Perl as a general programming language to manipulate text. However, with the pace of time, Perl became popular in other areas too like web development, GUI development, network programming and system administration.

Features of Perl

Following are some significant features of Perl:

  • DBI (Database Integration) interface of Perl supports third party databases such as Oracle, MySQL, Sybase, Postgre, etc.
  • Perl is compatible with most of the commonly used mark-up languages like HTML and XML.
  • Perl provides support to Unicode Standard, irrespective of the platform you are using.
  • There is a Perl Interpreter, a subset of Perl package, which can be embedded by the users in other systems.
  • Perl is a flexible programming language that provides support to both procedural and object oriented programming.
  • Perl can be interfaced with C or C++ Libraries through XS or SWIG (SWIG is a tool, which connects C/C++ programs with high level programming languages and other scripting languages.).
  • Perl, as a web programming language, can manipulate text and provide support to web development cycles.
  • Perl can manage encrypted web data and E-commerce transactions, and when embedded into Web Server it improves the processing speed of an application.
  • CPAN (Comprehensive Perl Archive Network) comes with a wide range of useful third party modules.

Perl supports the following features of procedural language: variables, brace-delimited code blocks, expressions, control structures, sub-routines and assignment statements (commonly used in C programming language). Apart from these, you will find some useful features of Shell, awk, Lisp and sed in Perl. In Perl, all variables are marked with leading "sigils" (it may be $/%/@) to identify variable names and also to interpolate the variables into strings. This is one of the most common features of Shell programming.

Perl contains several in-built functions to support common tasks and to access other useful facilities of the system. "List", "Associative Array" and "Regular Expressions" are some other significant features, which are taken from awk, Lisp and sed. These features of Perl provide support to text manipulation and data management process.

The concept of "reference", "package" and "class methods" are incorporated in Perl to simplify the complex data structure and functions, which are generated while executing a program. Memory management (managing computer memory) and categorizing data - are done automatically in Perl programming language. The interpreter of Perl takes care of this in the program and assigns or frees the storage for each object. Moreover, during execution of a Perl program data type conversions are done automatically.

Perl Modules

A Perl module is an independent Perl code, which users can use while developing Perl programs. Perl modules are similar to C library or C++ class. All module names are unique and provide specific functions. Each module can have several components and therefore, "::" sign is used to distinguish a component from its module. Following are some modules and their functions:

  • Math::Approx is a module that finds the approximate the values by a function. String::BitCount is another module that counts number of one bit in a string.
  • For matching and substituting string you can use String::Approx.
  • Math::Complex function is used to manage complex number data types.
  • The details of Perl build configuration are stored in the Config module.

The structure of Perl modules is flexible and developers can customize the modules by adding more than one component. All Perl Module Files and Library Files are defined by extensions ".pm" and ".pl", respectively.

The following list shows some of the significant modules of Perl programming language:

  • AnyDBM_File -- is a virtual class, provides necessary framework for multiple Database Managements - e.g., NDBM_File, DB_File, SDBM_File, ODBM_File, GDBM_File and other DBM implementations
  • AutoLoader: -- a module, which loads sub-routine based on the requirements
  • AutoSplit -- a module to split Perl package for auto loading
  • B -- compiles Perl program as a module
  • Benchmark -- the function of this module is to benchmark running times of Perl codes
  • CGI -- this is a class, which stands for Simple Common Gateway Interface
  • Config -- the module is used to access information about Perl configuration
  • Encode::Encoder -- this is an object oriented encoder module
  • Exporter -- this module implements default import method for other modules
  • Fatal -- the module is used to replace a function with its equivalent
  • File::Compare -- module that compares Perl files and file handlers
  • File::Copy -- module that copies Perl files and file handlers
  • File::Path -- module to create or remove directory tree
  • HTML::Form -- is a class that represents stands for HTML forms
  • HTML::Entities -- module, which encodes or decodes strings with HTML entities

Apart from all these modules there are several other useful modules in Perl to simplify the task of the developers.

Perl Data Types

Perl follows three data types - scalar, list and hash.

Scalar: Data that contains single value are known as "scalar". It can be any number, string or reference.

List: These data types are used to define any collection of single value data or scalars. The concept of "array" is used in Perl to define "list".

Hash: The term "hash" is used for mapping strings to scalars. In general, "hash" is an abstract data type, which is a collection of keys and values. For Perl programming language, "hash" is a collection of "strings", i.e. keys and "scalars", i.e. values.

Perl data types are identified by the sigils or symbols associated with them. Here "$", "@" and "%" are used to define "scalar", "list" and "hash" data types, respectively.

Getting started with Perl

Following is an example of basic Perl program, which you can run on UNIX platform after installing the latest version of Perl (Perl 6):

#!/usr/local/bin/perl
# Program to understand how Perl works.
print "This is a basic Perl program";
# Print a message.

In the above Perl program #!/usr/local/bin/perl is the first line of the program followed by several comments. Comments are written with # sign at the beginning. While executing a Perl program the system ignores all the comments, which are written in the program. In the above example, "# Program to understand how Perl works" and "# Print a message" are two comments. Only Perl statements are considered to build programming logic. The statements written in Perl language are similar to that of C and C++. All the statements end with the sign ";". In the above example print "This is a basic Perl Program"; is a statement.

After successful execution the above Perl program will print the following in the console:

This is a basic Perl Program

How to install Perl

The procedure of installing Perl varies based on the operating system that you are using. Perl is compatible with UNIX, OS/2, Windows and Macintosh.

For UNIX and OS/2 operating system you can directly download the latest Perl programming source code from perl.com.

After installing Perl in Unix operating system you can store the programming language in the following directories:

/usr/bin/perl
/usr/local/bin/perl

Similarly, to install Perl in Windows operating system, you have to download i86 Release Binary from the site.

After installing Perl in Windows operating system you can store the programming language in the following directory:

c:\perl\bin\perl.exe 

Advantages and Disadvantages of Perl

Perl comes with wide array of in-built functions to manipulate character string. Thorough knowledge is necessary to create a program by optimizing the number of string codes. Since Perl is platform independent, therefore developers or users can easily transfer Perl codes from one platform to another. Though people are excited by the portability of Perl programming language there are some limitations. It has been observed that some Perl functions, which depend on the underlying operating systems is hard to transfer from one platform to another.

Slow execution of Perl is considered as a drawback of this language. Before executing any Perl program you need to load an interpreter to read and interpret Perl codes and this procedure increases the runtime of the program. The flexibility of Perl programming allows you to apply dynamically loaded libraries in a program. There is also a negative impact on a Perl program that uses dynamically loaded libraries. Since the system will take more time to read the dynamically loaded libraries from the disk, therefore executing a program may take some extra time.

Error handling is often difficult for Perl programming language. It has been noticed that Perl can execute a program without considering minor errors. Therefore, after executing a program it becomes a hurdle for developers to track all the minor errors.

Add Feedback