What is Perl? How relevant it is and how to get started!

To begin with, we need to understand that Perl is not a programming language by itself, but rather a group of programming languages which are interpreted and dynamic. The family includes Perl 5 and Perl 6 and development of both languages take place independent of each other. While it is said that Perl is not an acronym, some refer to it as ‘Practical Extraction and Reporting Language’.

It combines the features of languages like C with shell script’s ability to execute large sets of commands. It also has elements of awk and sed, which is largely used in command line scripting to manipulate text. Programmers can use Perl for developing a variety of software applications. However, Perl is an interpreted programming language. The code written in Perl is compiled into byte-code. The byte code is converted in the machine instruction when the program is executed. The feature makes it easier for programmers to run and evaluate Perl applications.

From my experiences, it is best summarized as how my friend put it: ‘Perl is what happens when Python and shell scripting had a child’. Although, to be fair, Perl first appeared in the 1980’s and is older than Python, one can’t blame the person for thinking like that. Perl was once regarded as the ‘Swiss Knife’ of programming language, a tribute to it’s versatility. It had the ability to attract users who weren’t that good at writing code or just starting out in code, as well as the power users who could build powerful programs.

One of the biggest challenges in the 1990’s was the growth of the World Wide Web and the large amount of text based information during that period. As one of the languages very capable of text manipulation and undergoing rapid development, Perl was suited to the task at hand. As a result it was one of the most popular web programming languages out there, even being referred to as the ‘duct-tape of the Web’.

The decline of Perl

However, with the rise of Python as an alternative, this lead to the decline of Perl. One of the problems it faced was that scripts made in Perl are often messy, and aren’t as clear as Python. One of the mottoes of Perl is ‘There is more than one way to do it’, as opposed to Python which more often that not directs you to the solution. While that is good for the power users, with regards to the flexibility of solutions, most new programmers prefer the cleaner code and the direction Python gives as opposed to Perl. So Perl has lost ground in areas like genomics where the leading research in the field was done in the 90’s.

Why Perl?

In saying that, neither is Perl irrelevant nor is Perl terrible compared to Python. Perl offers such advantages compared to Python:

Speed: Perl is faster than Python for many tasks, and more powerful.

One-Liner: Perl has shortcuts which allow you to write quick scripts.

Regular expressions: They are first-class data-type rather than an add in. This means you can manipulate them programmatically like any other first-class object.

How relevant is Perl?

As for the question of relevancy, it remains relevant enough as it acts as a glue language which integrate various components or interfaces seamlessly. The design of Perl is influenced by shell script which is a widely used glue language. It makes it easier for programmers to integrate third-party interfaces or components which are not compatible with each other.

Perl was designed specifically for text processing. It built-in text processing capability makes Perl as widely used server-side programming language. The web developers can use Perl for both text processing and manipulation. Also, the database integration interface provided by Perl supports several widely used relational database management systems (RDMBS) — ORACLE, MySQL, Sybase and PostgreSQL. All in all, Perl may not be as popular as before, but it’s presence cannot be ignored.

Getting Started: The semantics

Let’s start with the classic Hello World program:

print “Hello, world\n”;
Output:Hello, world

Simple is as simple does.

Now, to declare a variable:

$str=”My name is Akshay”;
print “$str\n”;
Output: My name is Akshay

Like Python, you don’t have to explicitly declare the data type of the variable before hand.

Let us look at how to declare arrays:

@ages=(25,30,40);
@names=(“Jake”,”Logan”,”Felix”,”Ryan”);

Pretty neat, huh? Remember the special character that is used before the variable name is crucial here. Dollar sign for simple variables, and asperand for arrays.

Now let’s look at hash tables. For those who do not know, hash tables basically is a mapping of a key (a unique value) to a value.

%data = (‘John Paul’, 45, ‘Lisa’, 30, ‘Kumar’, 40);
print “\$data{‘John Paul’} = $data{‘John Paul’}\n”;
print “\$data{‘Lisa’} = $data{‘Lisa’}\n”;
print “\$data{‘Kumar’} = $data{‘Kumar’}\n”;
Output: $data{'John Paul'} = 45
$data{'Lisa'} = 30
$data{'Kumar'} = 40

— — — — — — — — — — — — — — — — — — — — — — — — — —

@days = qw/Mon Tue Wed Thu Fri Sat Sun/;
print “$days[0]\n”;
print “$days[1]\n”;
print “$days[2]\n”;
print “$days[3]\n”;
print “$days[4]\n”;
print “$days[5]\n”;
print “$days[6]\n”;
#Size of Array
print “Size: “,scalar @array, “\n”;
Output: 
Mon
Tue
Wed
Thu
Fri
Sat
Sun
Size: 0

Here we see two things: We should use dollar sign to indicate individual element of an array or hash table. Also using ‘scalar’ enables us to get the size of an array.

Finally, a quick and simple subroutine (which we normally refer to as functions in other programming languages):

#Subroutines- kind of like a function
#Use subroutine_name(list of arguments);
sub Hello
{print “Hello World \n”;
}
#Functional call
Hello();
Output:
9 May Wed
Hello World!

— — — — — — — — — — — — — — — — — — — —

sub Average
{ $n=scalar(@_);
$sum=0;
foreach $item (@_)
{
$sum+=$item;
}
$average=$sum/$n;
#my operator makes variable private
my $b=10;
print “Average for given numbers: $average\n”;
return $average;
}
$a=Average(10,20,30);
print “$a is average,$b”;
# $b doesn’t get printed
Output:
Average for given numbers: 20
20 is average,

Conclusion

This article was meant to whet your appetite in the programming language family of Perl. There are tons of resources available to go through. Remember that Perl is already available for those who use Linux. To execute a Perl file, save the file in the .pl format, and run it in the format:

perl filename.pl

So do try it and have fun!! Because you miss hundred percent of the chances you don’t take! Even if you don’t find it to your taste, at least you showed a willingness to try!

If you like this article, do follow me for more content like this! Also checkout my code and see my personal interests here.