Ads

Friday, December 26, 2008

Introducing Perl

Perl (Practical Extraction and Report Language) is a script language originally developed by Larry wall. perl is optimized for string manipulation, I/O, and system tasks. It has a very large set of built-in functions for a variety of tasks. In perl, there is no need to explicitly declare variables and you don’t also have to pre-declare the size of strings or of arrays. All variables have predictable defaults values until explicitly set. Perl is an interpreted language in the sense that your perl program’s source code can be directly executed. You don’t have to compile your program explicitly, but when you run your program, it is first compiled into byte code, and then converted into machine instructions as the program runs. Hence, perl basically lies between compiled and interpreted languages.


First Perl Program

Let us start with writing the traditional Hello World program in perl.

#!/usr/bin/perl
#
#The Traditional Hello World program
#
print ‘Hello World’; #print the string on
standard output

The first line of every perl program tells the machine what to do when this text file is executed and where the perl binary is located in the system, so that the machine can execute the text file through perl. After the first line, anything starting with # is treated as comments, and everything else is a perl statement, which must end with a semicolon. The print function is used to output information to a standard output or to a file. In our case it prints out the string ‘Hello world’ to the standard output. Type the above program in your choice of editor and save it. Now you have written first program in perl. To run this program, first change its mode. To make the file executable, use the following commands:

chmod +x filename

Where filename is the name of the file. Finally, run the program using one of the following commands;

perl filename
./filename

This is all you have to write to run your first perl program.
In next section we will look at the basic elements of the perl.

No comments: