Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Perl Programming Language: Basics Constructs

In this article, we will take a look about basics constructs in Perl Programming Language. We will also have an introduction of this powerful language.


Practical Extraction and Reporting Language, in short Perl, is a very well-known scripting language with an extremely versatile syntax and a rich collection of features. This language combines the power of Unix shell scripting and flexibility of Lisp to provide a relatively more powerful development environment. The beauty of this language is that it is easy to learn and faster than other languages in code development. Also it includes feature like object-oriented programming which gives an edge over other language. In this tutorial, we will cover some basic concepts and features of this language.

Key Features of Perl

Perl has features like easy learning, powerful text manipulation features and cross-platform availability. It is an open source platform so independent from commercial interference and have active online communities. Some of those features are given below:

  • Perl has an simple syntax and it makes it quick to learn. Either you are a new programmer or an hard-core, you’ll face no difficulty to understand the concepts of Perl.
  • Because Perl is a cross-platform language, it is very easy to write a Perl program on one platform and run it across different platforms with little or no modification.
  • Because of its versatility, Programmer can learn the language and adapt their own coding style.
  • It provides a regular expression engine which is capable of almost any type of textual transformation. This feature is the one reason for its popularity between programmer for both command line and we programming.
  • Perl has a very comprehensive library of modules which provides a very support for XML, Graphical user interfaces and embedded Perl scripting.
  • Perl has a very powerful and flexible object-oriented programming syntax at the same time very simple.
  • It supports multiple platforms such as Linux-Unix, Microsoft DOS and Windows and Apple Mac’s as well as some mobile platforms also.

Values and Variables

Values are a single data which is used in some expression to generate new value or some kind of result. 1066, 3.1415926 or “Hello” are some examples of values. To change or manipulate the values, we need variables.

A variable is a place where we can store values. Variables have a name through which we can read or change the value stored in that variable. In Perl, a punctuation symbol is prefixed to the name of a variable to indicate its data-type. Unlike C language, Perl only concerns about only the storage type: scalar, array or hash, instead of concerned about the data-type. Scalar variables are prefixed with a $ and store a single value where arrays and hash can store multiple values. A variable name can consist of only alphanumeric characters with underscore character. Variable names cannot start with a number. Some valid scalar variable manes are given below:

  • $A_Scalar_Variable
  • $scalarNo8
  • $_private_scalar

On the other hand, these names will create trouble for Python:

  • $64bitint (leading numbers are not valid)
  • $file-handle (minus sign is not valid)
  • $excangerateto£ (pound symbol cannot be used)

Operator and functions

Operators are the tools which are used to process the values and variables to generate new values. The numeric operators include the following:

$num = 6 + 5; 					 # addition operation
$num = 2 / 3;			 		 # division operation
$num = 6 % 5;					 # modulo division
$num = 2 ** 0.5; 				 # exponentiation
$num = -4; 					 # negative 4 is -4

Strings have a special concatenation operator to concatenate multiple strings:

$string = "P" . "e" . "r" . "l";

There are other operators for value comparison such as = and == for numbers and some of their equivalents (lt, ge and eq) for strings:

$Less_Than_Hundred= $number < 100;
$middle_or_higher = $string ge "middle"

The values on which operators operate are called operands.

Perl has many built in functions. Sometimes these functions can be used as operators. The print function is a very good example of these built-in operators, because it will print any number of values given to it:

print "A","list","of",5,"values","\n";

Perl has a lot of built in functions which are either scalar operator (take single value to process) or list operator (take either a fixed or arbitrary).

Data Types

The data types of Perl are divided into three main categories: scalars, arrays and hashes (associative arrays of other languages). These three categories are described as follows:


In Perl, scalar data types are solitary values, such as a string or a number. Unlike other strictly typed languages such as java or C, Perl has no such function to define the type of a scalar. Different scalar types are converted to each other by Perl automatically when they are used. Some examples of scalar types are given as follows:

$scalar_int = 72;
$scalar_fp = 5.5;
$scalar_str = "Sequence of characters";


Array is the second type supported by Perl. An array is a collection of variables with a consistent order. These values are associated with index values. An @ is prefixed with the array name:

@array1 = (4, 3, 2, 1);
@array2 = ('one', '2', 'three', '4', '5');

The first index values will be always zero and then so on. To access the elements of an array, index of element is provided into square brackets. Now the @ sign will be replaced by $ sign:

$fifth_element = $array[4];

There are various functions such as push, pop, shift, unshift, splice and reverse for array manipulations in Perl.


Hashes are similar data types to the Python’s dictionary. Sometimes, called associative arrays, it holds data as a pair of key-value. A % sign is prefixed before the names of hashes:

%hash_example = ('Mouse', 'Jerry', 'Cat', 'Tom', 'Dog', 'Ripper'); 

Here Mouse, Cat and Dog are keys and Jerry, Tom and Ripper are their associated values. Hash keys can only be a string type value.

A value stored in a hash can only be accessed by using its associated key:

$canine = $hash{Dog};

Unlike the elements of array, key-value pairs of hashes are not ordered. It means, it can be possible that the values will not be stored in same order as they input. It means they don’t follow any left to right order.


Subroutines in Perl is a code block which can be reused multiple times. They are defined by a keyword sub. Subroutines in Perl are like function in C or method in Java. The subroutine can be called using their names. An example subroutine is given below:

sub sample_routine {
print "A simple subroutine\n";

To call this subroutine, we can use any of these:

& Sample_routine;

This simple example is neither accepting any parameter nor returning any values.

Mostly Perl subroutines allow passing some parameters to perform some operation and then return some value reflecting the result. An example subroutine is given here which pass a number as parameter and then calculates its factorial and return the answer:

Listing 1: Representing an example Perl subroutine

$number=<>; 						# read a number from the keyboard
chomp $number; 					# remove linefeed
#subroutine is called with $number
# The subroutine
sub fact_example {
$input = shift; 						# read passed argument
# return zero immediately if given 0 as input
return 0 if $input==0;
# otherwise do the calculation
foreach (1 .. $input) {					 # '..' generates a range
$result *= $_;
return $result;
print "$number factorial of the number is $factorial\n";

Modules and Packages

The subroutines and variables which are reusable in Perl are stored to form a collection. This collection is called a module. We can include the needed module with a use keyword in our program. A module is just like header files in C language. For example, to include English module in our program, we write:

use English;

A package is a logical subdivision of compiled code In which variables and subroutines can reside. It is possible that two subroutines or variables can exist with the same name at the same time, if they are in different packages. Normally a package is created by a module with the same name, with nested namespaces separated by semicolons. So, sometimes packages and modules are treated as synonyms or at least connected. For example:

Use Scalar::Util;


In this tutorial, we tried to introduce some most common features of Perl language. Perl has many more features except these described here. It is very difficult to cover all of those topics in such a short article but this material provides you the look and feel of the Perl language. To learn more about this lovely language, refer to some good text books and do lot of practice.

Have total 6+ years of experience in developing enterprise applications using DOT.NET 3.5 and 4.0(C#, VB.NET, ADO.NET, ASP.NET),java, JQuery, JSON, LINQ,WCF, MVC3, MVC4, Silverlight, SQL Server, mobile applications and Oracle etc ...

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login