Perl For Beginners
Perl For Beginners Tutorial
This perl tutorial is taken from The Linux Documentation Project and is licensed under FDL. You are free to reproduce it so long as credit is given. Original document created by Guido Socher. Changes made for formatting to fit in with this wiki.
What is perl?
Perl is a script language which was originally developed by Larry Wall. The source code can directly be "executed" using perl and there is no explicit compilation step involved. This perl program is usually installed in /usr/bin/perl. Perl is in many aspects quite similar to the classic unix programs awk and sed but perl has gone a long way from there. Today you can even do object oriented programming and design graphical user interfaces with perl. Perl can easily be extended in its capabilities with libraries. The perl archive at ftp://sunsite.auc.dk/pub/languages/perl/CPAN/ has many of them. This first article will however not go into advanced topics. Instead I would like to show you some basics and have more advanced things in later articles.
Perl is a very useful scripting language. It is a universal tool for everyone with some programming skills.
A simple program
Save the following program under the name my1stprg and then make it executable with the command chmod 755 my1stprg . Run the program with perl my1stprog and see what it does.
Let's look at the code. The first line is not a comment. This type of line
starting with "#!" is typical for unix scripts and it tells the operating system what to do with this text file.
Any line after the first line starting with a #-character is a comment
line (see e.g line 4). We can also see that all statements are terminated by a semicolon. A variable starts with a dollar sign ($). Perl variables can take strings, integers and floats. The data gets converted automatically to the right type
dependent on the context in which a variable is used. The $-variables are called scalar variables. Perl has also arrays (starting with @ instead of $) and hash
tables (starting with % instead of $). Theses types of variables will be
discussed in a future article.
The print function on the second line of our program writes a text string to stdout. It is similar to the echo command in shell scripts and the variables get expanded to their values if the string is enclosed in double quotes. Variables inside single quotes do not get expanded and a backslash may be used to quote special characters such as the dollar sign or a quote inside quotes. Here is an example:
This will produce:
Back to our first simple program. The line $name=<STDIN>; promts the user for input and waits until he/she typed the return key. After that line the variable $name holds what the user typed including the terminating newline character. The command chomp($name); removes this new line character from the variable $name. Finally the length function counts the number of characters in $name.
Unfortunately our program has a bug. What happens if you type
a tab or space after your name? It will be counted as well. How can we fix it?
What we need is some find and replace function that removes any white space from $name. Perl is very good in manipulating text strings and offers exactly this. [ \t] is a regular expression that matches space or tab. In perl this can also be abbreviated and written as \s. The perl substitute command takes a regular expression and has the syntax: $name=~ s/regexp/replacement/g; If we leave the replacement string empty then all occurrences where the regular expression fits will be replaced with nothing. Now our program looks like this:
This is not a very useful program as it does not have any control statements (loops and if statements) but it gives you a basic idea. This series will be continued and we come to more complex programs soon.
You can try "man perl" and "man perlfunc" to see what functions are available. All this is however more reference material and you should follow this tutorial or read a book such as "Learning Perl" from O'Reilly to learn perl.