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

A good developer is a lazy developer. In IT, laziness is a gift (would you believe). If you've done something before you should not have to do it again. Or else, maybe it's time to write a script or some lines of code to automate the process so that you won't ever have to do it again. A good developer spends a lot of time thinking about his tools, and constantly improves his environment by developing scripts and tools that will help him develop faster.

In the previous lessons we've used a lot of functions that were built-in the language. For instance, we used "split", "print", "chomp" ...etc. What we haven't done yet, is to define our own functions. In Perl we call them subroutines.


A subroutine is basically a few lines of code you give a name to. You can then run these lines by simply calling the subroutine from your script. You can pass arguments from the scripts to the subroutine, and depending on the subroutine it can return a result or not.

If you've never used any programming language before, you probably don't know what to think right now. But don't worry, it's very simple. Let's look at an example to make things a little bit clearer.

Have a look at the following script:

As you probably guessed, this script does something pretty stupid. It shows you the lines which contain "house" and then the lines which contain "dog" within the file /etc/myProgram/myFile. It's not what it does that is shocking though, it's how it does it.

Look at the code. It does exactly the same thing twice... once for "house" and once for "dog". If there was a bug in the way it shows the lines containing "house", would I remember to also fix the same bug for "dog" when I fix it? Of course not, I'm only human after all.

The idea is simple. Instead of showing the lines which contain "dog" or "house", we'll define a subroutine which can show lines containing "something". And then we'll call this subroutine with "dog" and "house".

As you can see, we reduced the amount of code and more importantly, we removed code that was repeated twice. The first part of the script defines a subroutine called showLines. The second part simply calls it twice, once giving it the argument "house", and then giving it the argument "dog".

Within the subroutine, we simply replaced the references to "dog" and "house" with a variable representing the argument. When you call a subroutine with an argument or a list of arguments, the subroutine is executed and the arguments are stored in the following array variable: @_.

The line my ($keyword) = @_; is simply assigning the values of the arguments to an array containing variables. In this case the value of the argument is assigned to $keyword, which receives "house" and then "dog" when called from the script.

Local variables

The reason why the assignment is preceded by "my" is because we want the $keyword variable to be local to the subroutine. When a variable is defined with the keyword "my"it becomes a local variable and it only exists within the subroutine. This way, we do not have to worry whether or not another variable with the same name is defined in the script. It's always a good idea to make variables local inside your subroutine, so always declare them with the "my" keyword.


Let's go further. Instead of a subroutine which prints the lines containing "something" from the file /etc/myProgram/myFile, we'll define a subroutine which prints the lines containing "something" from "some" file. This time we'll use two arguments: the file to look into and the keyword to search for.

We could even define a subroutine which searches for a keyword in a list of files. For this we would pass two arguments: a keyword, and an array containing filenames.

In this example, note how the subroutine retrieves the arguments. The array @_ initially contains two objects, a scalar and an array. By using the "shift" Perl function we retrieved and deleted the first element of the array and assigned it to the local variable $keyword. We're then left with @_ only containing the array of filenames.

Returning something

Subroutines can return values. In our previous example, our subroutine was returning nothing. We were simply calling it and it was outputting some lines on the screen. In this example, we'd like the subroutine to do the same, but also to count the number of lines it found and to return it.

As you can see, the subroutine simply returns the number of lines by using the "return" keyword. In the script, the return value is assigned to the variable $nb.


Thanks to subroutines you can now define code which can be called more than once. Through the use of arguments and return values, you can make your subroutines more flexible. By extracting common behavior and defining it within subroutines you'll reduce the cost of maintenance in your scripts and the number of eventual bugs it might contain. In most cases this will also make your scripts more scalable. In the next lesson we'll look at how to define our subroutines outside of our script, so that they can be called and used by more than one script.

Lesson 5

Rate This Article: poor excellent
Comments about this article
writen by: hari on 2007-06-19 16:21:44
thanks a lot for your 6th lesson .. the tutorial is of great help 4 me to larn perl easily.. i never thought i could learn it so fast.. waiting for you next lesson..
RE: Mr written by hari:
The formatting of code samples are not n
writen by: Manilal on 2007-10-10 07:50:46
The formatting of the code samples are npt neat. It would be nice if they are wrapped in HTML pre tags.
RE: The formatting of code samples are not n written by Manilal:
Perl lessons 7 - 10
writen by: aravinda on 2007-11-30 01:49:09
Hi where can i find perl lessons 7 - 10 Thanks Aravinda
RE: Perl lessons 7 - 10 written by aravinda:
Eagerly awaiting Lessons 7-10
writen by: Automation_Man on 2007-12-03 16:12:25
Well presented and thanks so far
RE: Eagerly awaiting Lessons 7-10 written by Automation_Man:
Lesson 7, 8, 9 and 10
writen by: Faruk Ahmed on 2007-12-17 15:59:21
Your lessons are wonderful. As a new learner of Perl, I find very helpful. Please provide the next lesson # 7, 8, 9 and 10. Thanks. Faruk Ahmed
RE: Lesson 7, 8, 9 and 10 written by Faruk Ahmed:
writen by: pavan on 2008-06-04 07:14:51
Hi, Your lessons are easy to follow as a beginner to perl i feel very confortable.plz post remaining lessions 7,8,9,10. Thanks, Pavan
RE: thanks written by pavan:
Oracle consultant
writen by: SS on 2008-06-22 08:24:12
Where can I find Learn Perl in 10 easy lessons from Lesson 7 to 10 Please Thanks
RE: Oracle consultant written by SS:
Im waiting for another lessons
writen by: pseudoweb on 2008-11-21 10:28:55
Your tuto is really great. But im waiting for lessons 7 8 9 10
RE: Im waiting for another lessons written by pseudoweb:
writen by: mikey on 2009-01-08 12:18:00
Yeh, really good thanks! Let me join the others in asking for lessons 7-10.
RE: Lessons written by mikey:
Different Link
writen by: jedsch on 2010-07-29 15:17:12
Maybe you should check the following: http://www.cs.tut.fi/~jkorpela/perl/course.html for more Free Lessons.
RE: Different Link written by jedsch:
Thanks for your tutorials
writen by: rvikramr on 2012-07-24 09:29:53
Hi ,

I was amazed how quickly I could learn perl.
Thanks to your lessons.
please share with us link for lesson 7,8,9 and 10.

RE: Thanks for your tutorials written by rvikramr:
Thanks for your tutorials
writen by: rvikramr on 2012-07-24 09:29:54
Hi ,

I was amazed how quickly I could learn perl.
Thanks to your lessons.
please share with us link for lesson 7,8,9 and 10.

RE: Thanks for your tutorials written by rvikramr:
RE: Thanks for your tutorials
writen by: Neelisharma on 2012-09-23 20:46:24
Did u get the reaming lessons by any chance?
Reply to Neelisharma:
Do u have lessons 7 8 9 10 please
writen by: Neelisharma on 2012-09-23 20:45:21
Thank you very much for the tutorials, they are great. Do u have lessons 7 8 9 10 please?
Thanks again
RE: Do u have lessons 7 8 9 10 please written by Neelisharma:

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