Perl ProgrammingIntroduction to Perl Programming Language

Introduction to Perl Programming Language

After talking about the Python language as the first of three possible representatives for the P layer of the LAMP stack, it is time to tackle the next instance, Perl.
 
What is Perl?
Perl stands for Practical Extraction and Report Language. Perl was developed in 1987 by Larry Wall. It is classified as high-level, general-purpose, interpreted programming language.
Perl was designed to be an efficient alternative to the UNIX/Linux tools: grep, awk, and sed. Besides to being a rich text-manipulating language, it is a powerful scripting language as well.
 
Why Perl?
– OK… you claim it is a good and efficient language. Tell me why I should take my decision to spend time learning Perl?
 
To evaluate something, you need to now its features. Here are the basic features of Perl:

  • Perl is completely free: Perl is an open source language, which is ruled by the General Public License (GPL).
  • Availability: Perl comes pre-installed with most of the UNIX and Linux distributions. So, no need to care about downloading the source, and installing it. For Windows and Mac OS systems, the installation package is available for free on the official site of Perl: https://www.perl.org

perl download
 
In the next section, we will see how to install Perl on Windows.

  • Flexibility: like other high-level programming languages, Perl is really flexible.
  • Interpreted: No compilation or linking steps required. Just, write and execute.
  • Platform Independent: The same code that executes on a UNIX or Linux box may run unchanged (or with minor modifications) on a Windows machine.
  • Easy to learn: within few days, you could be able to write your own Perl scripts that achieve much of your work needs.

 
Or, the best way (in my opinion) is to let Perl talk about itself, and tell you about its strengths. Are you ready for that?! OK, here we go!!!
 
Installing Perl on Windows
The most popular Perl software for Windows is Strawberry. To get it, click “Strawberry Perl” link in the web page shown in the figure above. This will take you to the following page:
Installing Perl on Windows
 
Click the suitable version to download it. Once downloaded, the installation procedure is quite straightforward.
 
Verifying and Locating Perl Installation on Linux Systems
To verify that Perl is installed on your Linux box, use the rpm command with –qa option to query all the installed packages:
Verifying and Locating Perl Installation on Linux Systems
 
To locate the executable binary file for the Perl interpreter, use the which command:
1
 
In most of our examples in this series, we are going to work on a Linux environment.
 
Your First Program
Here is the moment you are waiting for!! Writing our first Perl script. As usual, we are going to write a so simple script that prints a string of text. In your system, open your favorite text editor and type this one line program:
2
 
Save, and exit the file. Now, execute it as follows:
3
 
Hurray!!! We have written a Perl program, and execute it!!!
 
Although it is very simple script, several points can be noticed and concluded out of it:
 
First: to print some data (such as a string of text) to the standard output (screen), the print statement is used.
Second: just like the majority of programming languages, Perl uses double-quotations to delimit strings.
Third: To print a new line, you have to explicitly mention it (notice the usage of ‘\n’ character at the end of the printed string.
Fourth: to execute a Perl script, the Perl interpreter is called and passed the name of the script file to be interpreted and executed.
 
Learn the Basics of C Programming Language

Another Way to Execute the Script
Have you already guessed what is it?! Bravo, it is the Shebang statement…
The Shebang is a special statement to tell the program loader to execute the language interpreter. Most of the time, it is seen in the first line of any script file. The syntax of the Shebang is common:

#!/pathto/language/interpreter

So, let’s re-write our first script using the Shebang statement.
1) First, we need to locate the executable file of the Perl interpreter (using the which command as we did above)

[root@server01 ~]# which perl
/usr/bin/perl
[root@server01 ~]#

2) Next, edit the script file, and add the following line above the print statement:

#!/usr/bin/perl

Now, the script should appear like this:
4
 
3) After saving and exiting the file, we should make it executable. In UNIX/Linux systems, the chmod command is used:

chmod a+x hello.pl

4) Now, we can execute it just like any other UNIX/Linux executable:
5
 
Using Comments
In general, it is recommended to use comments inside your scripts wherever you should do. Using comments helps make your code more readable. Comments are meaningful strings of text which aim to document the software programs. Among possible information to include, the purpose of the script, the author of the script, and the date it was written may be specified in comments. In Perl, to mark a portion of text as comment, precede it with the hash ‘#’ character.

# This will be treated as comment.

When encountered, comments are totally ignored by the Perl interpreter. Now, let’s re-write the hello.pl script with some comments added:
6
 
Now, let’s execute it and see the difference:
7
 
No difference at all!! And there should never be. This is simply because (as I told you) comments are ignored by the Perl interpreter.
 
* * * * * *
 
In this article, we had an introduction to the Perl language. We have seen that Perl is a high-level, general purpose, interpreted programming language, with many great features: it is free, interpreted (no need for compilation), available for many operating systems, and platform independent.  We have written our first program, and seen how to execute it in two ways: either by invoking the interpreter and passing the script file to it as an argument, or using the Shebang statement. Finally, we have learned how to use comments and their importance in making our source codes more readable.
That was Part one in a long series to come. So, stay here, and don’t go anywhere. There is more to learn… See you.

1 COMMENT

  1. Perl is a steady, cross stage programming dialect.

    In spite of the fact that Perl isn’t formally an acronym however few individuals utilized it as Practical Extraction and Report Language.

    It is utilized for mission basic undertakings in people in general and private parts.

    Perl is an Open Source programming, authorized under its Artistic License, or the GNU General Public License (GPL).

    Perl was made by Larry Wall.

    Perl 1.0 was discharged to usenet’s alt.comp.sources in 1987.

    At the season of composing this instructional exercise, the most recent form of perl was 5.16.2.

    Perl is recorded in the Oxford English Dictionary.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -