original in en Guido Socher
Guido is a long time Linux fan and recently he became editor of _LF_. These days he is also very busy renovating the house and planting salad and other stuff in the garden.
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 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.
Save the following
program under the name my1stprg and then make it executable with the
command
chmod 755 my1stprg . Run the program and see what it does.
#!/usr/bin/perl -w
print "What is your name?\n"; $name=<STDIN>; #remove the new-line character: chomp($name); $len=length($name); print "\nHello $name! Your name is $len characters long\n"; |
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:
#!/usr/bin/perl -w
$name="joe"; print "1 $name\n"; print '2 ', $name , "\n"; print '3 ', '$name',"\n"; print "4 \"$name\"\n"; print "5 \$name\n"; print "6 \"\$name\"\n"; |
This will produce:
1 joe
2 joe 3 $name 4 "joe" 5 $name 6 "$name" |
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.
(More about regular expressions can be
read here). 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:
#!/usr/bin/perl -w
print "What is your name?\n"; $name=<STDIN>; #remove the new-line character: chomp($name); print "\nHello $name!"; #remove spaces from the string: $name=~s/\s//g; $len=length($name); print " Your name is $len characters long\n"; |
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 also 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.