Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
Site Navigation
Linux Forums
Linux Articles
Product Showcase
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Free Publications

Thanks to the Perl scripting language you can automate various tasks in your Linux system. Learning Perl is both easy and fun, and you'll soon be able to write scripts which will make your life easier. In these series of articles I'll start by explaining the basics and I'll progressively introduce more complex concepts and advanced techniques.

I'll try to explain things as much as possible, so whether you are familiar to programming or not, you should find it easy to learn Perl and be comfortable with using this language after the end of this series of articles.History of Perl

Larry Wall created a scripting language in 1987, which he called the "Practical Extraction and Report Language". It was designed as a text-processing language for Unix operating systems. Various tools and languages already existed (Unix shells, sed, awk, C...etc) and programmers usually used many of them together. Larry Wall wanted the language to cover all aspects and needs related to text-processing so the programmer wouldn't have to use any other tool in conjunction with Perl. Also, he designed Perl as a language that was easy and fast to use but which would also allow the programmer to get into the innards of things.

Perl offered features no other language had offered before. It was filling a niche no other tool had, and it immediately became popular. In 1994, Perl 5 was released and at that stage it was a stable and well established general purpose programming language.

Particularities of Perl

Perl is truly unique. As we go along and study its different features you'll probably see that by yourself. Larry Wall applied a lot of his linguistic knowledge into the making of Perl. Some people even consider it a natural language. Its vocabulary is extremely rich and its grammar is very flexible. Perl programmers usually say "There's more than one way to do it". In fact, you can literally write a Perl script your way, with your own style. Some people even do "poetry" in Perl :) Because of this, some Perl scripts can be very difficult to read. Writing them though, is always a pleasure.

The Perl interpreter

Perl is an interpreted language. This means that the code you will write using the Perl language will need a program called the Perl interpreter in order to be run. For instance, if you write a Perl script in a file called myScript.pl (.pl is commonly used as an extension for Perl scripts), you will not be able to run it directly. You will need to call the interpreter to run it:

perl myScript.pl

In this example

myScript.pl is your Perl script and perl is the Perl interpreter. Installation of the Perl interpreter

The Perl interpreter is an essential tool and it is usuallyinstalled by default on most GNU/Linux distributions. Forinstance, the following distributions come with a recent and updatedversion of Perl:

  • Suse 10.1
  • Fedora Core 5
  • Debian Testing
  • Ubuntu 5.10
  • Mandriva 2006
  • Slackware 10.2
  • Mepis 3.4-3
  • Gentoo 2006.0
  • Knoppix 5.0

For an exhaustive list of distributions which include perl, you can search distrowatch.com: http://distrowatch.com/search.php?pkg=perl&pkgver=5.8.8#pkgsearch

To makesure the interpreter is installed on your machine and to see which version it is, you can open a terminal and type:

perl -v

If it is installed, this should print the version of Perl installed on your machine:

clem@pluto:~> perl -vThis is perl, v5.8.8 built for i586-linux-thread-multiCopyright 1987-2006, Larry WallPerl may be copied only under the terms of either the Artistic License or theGNU General Public License, which may be found in the Perl 5 source kit.Complete documentation for Perl, including FAQ lists, should be found onthis system using "man perl" or "perldoc perl". If you have access to theInternet, point your browser at http://www.perl.org/, the Perl Home Page.

If Perl is not installed on your system, you will certainly be able to install it through your distribution's package manager. Simply search for perl within your distribution's repositories, or start considering using another distribution. After all, Perl is an essential tool which should be included by default and regularly updated by your distribution: http://www.cpan.org/ports/#linux

Implicit use of the interpreter

The Perl interpreter is usually used to run Perl scripts which are written in a file. It also features an interactive mode which you can use by simply typing perl without any argument. However, in this lesson we will focus on using files.

In order to run a Perl script, you can call the interpreter with the file as an argument:

perl myScript.pl

... or you can tell the Perl script where to find the interpreter and make the script executable. This is common practice among programmers and you are encouraged to do so. Within the script file, the first line tells the shell how to interpret the file. This line basically shows the path to the Perl interpreter:


Note: The Perl interpreter is usually installed in /usr/bin, but your system might be different. To make sure, type "which perl":

clem@pluto:~> which perl/usr/bin/perl

Also make sure your Perl scripts are executable and have proper permissions:

chmod a+rx myScript.pl

Once the script is executable, it can be run directly. The shell looks at the first line of the file which starts with #!, and then it runs the interpreter which path is found on that line with the rest of the file. In other words, thanks to that little trick, you can run you Perl script directly:


Although you are not explicitly calling the interpreter here, keep in mind that it is actually run by the shell on your behalf and that it is the interpreter which runs your script.

Your very first Perl script!

You have your interpreter installed, and you're ready for your first script: a simple script that writes "Hello World!" on the screen (I know.. it's a bit useless, but it's more or less a tradition when you're learning a new programming language, so let's simply write "Hello World" and enjoy the lesson since it's still easy to understand :)).

Create a file called helloWorld.pl and write the following into it:

#!/usr/bin/perlprint "Hello World! n";

Make the script executable:

chmod a+rx helloWorld.pl

Run the script:


As you probably expected, "Hello World!" gets printed on the screen. The script only contains two lines and is quite easy to understand. The first line will always be the same; it tells your shell where to find the Perl interpreter. The second line is actually the only Perl instruction of your script, it tells the interpreter to print "Hello World!" on the screen. In Perl, each instruction finishes with a semicolon. If you're new to programming you'll probably forget semicolons in your code, so be careful with that. The reason for the semicolon is that an instruction can sometimes be long and take more than one line, so it is an effective way to mark the end of an instruction. Most languages use semicolons and once you're used to it, it seems very natural. You might also wonder what the "n" is for. It simply is a special character which corresponds to the new line character (as when someone presses the Enter key) so that the cursor goes the next line of the screen after printing "Hello World!".

In the next lesson we'll start using variables, opening files and do a lot of things which will come handy for you later. Now that you know what Perl is and how to use it, we'll start to focus on the language itself. Of course, in the meantime I'll be glad to answer your questions.

Lesson 2

Rate This Article: poor excellent
Comments about this article
Waiting for lesson 2
writen by: Mark West on 2006-06-13 21:43:09
Seemed like a pretty decent article to me. Can't wait for lesson 2.
RE: Waiting for lesson 2 written by Mark West:
RE: Waiting for lesson 2
writen by: SapnaBali on 2010-08-30 03:00:52
Want to read lesson 2
Reply to SapnaBali:
writen by: decepticon on 2006-06-14 15:04:24
Please, I am ready for lesson2, link?
RE: Lesson2 written by decepticon:
Lesson 2
writen by: Mark on 2006-06-14 15:10:28
Lesson 2 is now linked to in the bottom of the article.
RE: Lesson 2 written by Mark:
Unix consultant
writen by: raji murthy on 2006-06-28 09:36:38
I would like to leran writing perl scripts. Please send me the lessons
RE: Unix consultant written by raji murthy:
Jr. network admin
writen by: Richard Houck on 2006-07-18 14:14:02
I would like to leran writing perl scripts. Please send me the lessons.
RE: Jr. network admin written by Richard Houck:
Sr. Unix Admin
writen by: Deepak on 2006-12-12 16:41:04
I like to learn perl asap .
RE: Sr. Unix Admin written by Deepak:
Thank you.
writen by: Vkiperman on 2007-01-05 21:19:43
RE: Thank you. written by Vkiperman:
it didn't work
writen by: Richard Gill on 2007-01-05 22:05:03
no perlprint on my system: Gehus:~ richard$ perl -v This is perl, v5.8.6 built for darwin-thread-multi-2level (with 3 registered patches, see perl -V for more detail)
RE: it didn't work written by Richard Gill:
Page formating
writen by: k on 2007-01-05 22:25:00
Is there a reason this page scrolls horizontally forever! Maybe I can write a perl script to scrape the text from this page and put it in a readable format.
RE: Page formating written by k:
the lessions
writen by: Bob Loblaw on 2007-01-06 00:31:58
click this link - http://www.linuxforums.org/programming/ its got up to lesson 6.
RE: the lessions written by Bob Loblaw:
Perl is not interpreted
writen by: grimborg on 2007-01-06 09:15:54
Perl is not an interpreted language. What you call the "perl interpreter" first compiles and then executes the code. Check the perl docs. You can verify it by trying to run a file which has errors that can be detected at compile time: it will not run at all. If it were an interpreted language the file would be run up to the error.
RE: Perl is not interpreted written by grimborg:
writen by: cvb on 2007-01-08 03:54:34
RE: cvbvcb written by cvb:
I can read the the perl docs too (you sh
writen by: Kevin on 2007-01-18 21:25:29
"A computer scientist will correctly explain that all programs are interpreted and that the only question is at what level. But if youask this question of someone who isn't a computer scientist, they might tell you that a program has been compiled to physical machine code once and can then be run multiple times, whereas a script must be translated by a program each time it's used. Perl programs are (usually) neither strictly compiled nor strictly interpreted. They can be compiled to a byte-code form (something of a Perl virtual machine) or to completely different languages, like C or assembly language. You can't tell just by looking at it whether the source is destined for a pure interpreter, a parse-tree interpreter, a byte-code interpreter, or a native-code compiler, so it's hard to give a definitive answer here."
RE: I can read the the perl docs too (you sh written by Kevin:
writen by: a on 2007-01-29 07:52:43
[b]Bold Text[/b]
RE: a written by a:
writen by: ranjeeth p t on 2007-03-07 14:07:47
help me i want 2 learn perl
RE: perl written by ranjeeth p t:
page borked
writen by: me on 2007-03-19 08:04:46
This page is broken: it displays correectly in neither firefox nor IE. Fix plz.
RE: page borked written by me:
writen by: lalit on 2007-05-31 07:46:52
RE: kar written by lalit:
writen by: Himanshu Thapar on 2007-06-13 15:13:39
Hello, I tried the lesson 1 and I tried to write the hello world program but it tells me that I dont have right to save it.
RE: Perl written by Himanshu Thapar:
writen by: Senthil Kumar. P on 2007-08-06 02:49:08
I want to learn complete 10 lessons.
RE: PERL written by Senthil Kumar. P:
learn perl
writen by: Aruna on 2007-08-16 10:32:01
RE: learn perl written by Aruna:
Program Coordinator
writen by: Jean Girault on 2007-09-07 14:35:06
I would like to learn how program in Pearl language. What are the requirements.
RE: Program Coordinator written by Jean Girault:
learn perl in 10 easy lessons
writen by: Ellebanna M. Roa on 2007-10-01 03:29:34
I really need it to start my requirement in my A.I. subject. I think this would be a good start.
RE: learn perl in 10 easy lessons written by Ellebanna M. Roa:
I liked it!
writen by: Priya Viswanath on 2007-11-22 05:20:00
Want to finish the whole of the perl series. hope i will succeed! Thanks Priya Viswanath
RE: I liked it! written by Priya Viswanath:
Linux Administrator
writen by: Ashok on 2008-02-13 06:54:09
Hi I would like to learn perl , could you please provide me link so that i can learn online. Regards, Ashok
RE: Linux Administrator written by Ashok:
writen by: Sanjay Saha on 2008-02-17 00:25:40
Hello , I am Sanjay from india , i want to learn perl. Can u provide me link of perl tutorial
RE: Perl written by Sanjay Saha:
hi new user
writen by: Govindraj on 2008-02-26 13:46:16
RE: hi new user written by Govindraj:
writen by: GOVIND on 2008-02-26 13:49:52
RE: hi written by GOVIND:
how to perl
writen by: zafar hussain on 2008-04-10 13:12:32
Hi I am new be for perl scripting and want to learn please provide me healthy feed back for the same. Thanks Zafar Hussain
RE: how to perl written by zafar hussain:
writen by: kalesha shaik on 2008-05-23 12:22:55
your lessons are good and quite interesting
RE: kalesh written by kalesha shaik:
writen by: sarath on 2008-06-08 23:50:02
its very nice and i would like to your lesson pls give the lesson for me
RE: perl written by sarath:
writen by: avi on 2008-06-14 02:55:04
RE: perlproblem written by avi:
learn perl
writen by: senthil on 2008-06-26 05:33:56
I am in epublishing field. I want to learn perl through institution. Is't any institution take perl script in chennai.
RE: learn perl written by senthil:
writen by: Brian on 2008-08-01 14:40:02
RE: Lesson written by Brian:
First lesson
writen by: Andrew Pepper on 2008-09-20 09:07:35
Hi ..... I've tried the Hello World program and it won't run. In the shell console is says: linux-v9o7:/home/pepp/perl # ./helloworld.pl bash: ./helloworld.pl: Permission denied Have i done something wrong. I have tried it running in root but it prints the same message.
RE: First lesson written by Andrew Pepper:
writen by: Anonymous on 2009-01-02 08:24:16
RE: written by Anonymous:
how can run the perl script
writen by: krishnakant karaiya on 2009-01-30 23:52:01
I work on the linux system and learn perl but problem is that how i can run the script. I didnt understand how interpretor work while perl is installed default.
RE: how can run the perl script written by krishnakant karaiya:
A couple of mistakes
writen by: friez on 2009-12-16 11:34:47
Found a couple of mistakes on the code, it should look like this:

print "Hello World! \n";

the first line should only be the path to the interpreter, and the print statement should have the backslash on the 'n' so it is interpreted as a new line.

hope this helps
RE: A couple of mistakes written by friez:
this nice tutorial
writen by: cybermuttaqin on 2010-02-18 01:04:41
thank you very much
RE: this nice tutorial written by cybermuttaqin:
this nice tutorial
writen by: cybermuttaqin on 2010-02-18 01:04:43
thank you very much
RE: this nice tutorial written by cybermuttaqin:
this nice tutorial
writen by: cybermuttaqin on 2010-02-18 01:04:45
thank you very much
RE: this nice tutorial written by cybermuttaqin:
this nice tutorial
writen by: cybermuttaqin on 2010-02-18 01:04:46
thank you very much
RE: this nice tutorial written by cybermuttaqin:
Text editor ?
writen by: loftus49 on 2010-05-20 13:00:59
I guess that the "new perl file" and it's text is written using a text editor such as VI, PICO or GEDIT ? Is this correct?
RE: Text editor ? written by loftus49:
writen by: nathanroben on 2010-10-22 19:01:39
i really intend to learn perl but sometimes i find it somehow not simple
RE: hi written by nathanroben:
Thanks, Good Intro Article!
writen by: robhwill on 2011-03-05 05:37:41
Thanks for the good job! This article displays a regard for the concerns expressed by the 'newbie' who wrote (on this site) about the common "read the manual..." advice that flies around everywhere. I remember back in 1993 I took a programming course in C (not C ) and imagine my surprise when I realized that finding out not only what to read/look into, but probably more importantly, what *sequence* to study the materials was more than half the battle. Too many tech-related courses, books, and other materials omit the concrete 'you-are-here' and 'you-will-be-here/do-this' that is so important for 1st timers.
Keep up the good work...and again, thanks. Have A Healthy, Prosperous Day!
RE: Thanks, Good Intro Article! written by robhwill:
RE: Thanks, Good Intro Article!
writen by: robhwill on 2011-03-05 05:39:05
ooops! Edit: Course was 'C', not C . bye....
Reply to robhwill:
Reply to robhwill
writen by: robhwill on 2011-03-05 05:39:46
not C !!! doggone it!!
Reply to robhwill:
Reply to robhwill
writen by: robhwill on 2011-03-05 05:41:24
Okay (this bb software obviously *does not allow* 'plus' signs.....SO: (last time) course was C not C-plus-plus Ha!
Reply to robhwill:
Two lines in the example?
writen by: stuckl on 2011-07-31 19:38:45
Clarification please. You wrote:

Create a file called helloWorld.pl and write the following into it:

#!/usr/bin/perlprint "Hello World! n";

Then you say the script has two lines. Is this two lines in one? Or is the first line "#!/usr/bin/perl" ?

Sorry, it's a good article but I'm very new to perl.

RE: Two lines in the example? written by stuckl:

Comment title: * please do not put your response text here