Perl Tutorial

Perl is a programming language which can be used for a large variety of tasks. It is abbreviation of Practical Extraction and Reporting Language. It was developed in 1987 by Larry Wall.  A typical simple use of Perl would be for extracting information from a text file and printing out a report or for converting a text file into another form. But Perl provides a large number of tools for quite complicated problems, including systems programming, web development, GUI development.

Programs written in Perl are called Perl scripts, and the term the Perl program is the system program called Perl for executing Perl scripts.

Perl is an interpreted language . To run a program in Perl language, create a text document and tell the interpreter to run it as a program. The interpreter checks it, compiles it into machine code and runs it.

perl -v : to check  if Perl is installed in your machine

Running Perl

open a file finame.pl in vi editor

enter print “hello world\n”;

save file finame.pl [esc :wq]

To run the perl script use : perl finame.pl

Output will be hello world

Perl Variables

Perl variables are used for occupying memory location to store dynamically entered values.

There are 3 main types of Perl variable:

– Scalars

– Arrays

– Hashes

 

Scalars:

Scalars contains single value either numbers or text. It is represented by a $ sign.

Examples are:

$x = 5;

$var = $x +20

$file_name = “raj”

 




Arrays:

Arrays contains a set of numbers or texts. It is represented by a @ sign

Examples are:

@numbers = (1, 2)

@names = (“Rock”, “Balboa”, “Stallone”)

@values = ( $a , $b , 1, 7 )

Array elements can be printed with syntax $array[n] where n is the nth element of array

$numbers[0] = 1

$names[1] = Balboa

$values[3]=  7

Read more on:  Adding/Removing Elements in an Array

Hashes:

Hashes are special arrays and they use word as indexes in place of numerical indexes. It is represented by %

Also they use curly braces {} in place of square braces [].

It is defined with a key and value pair

$count{head} = 10;

$count{book} = 20;

$count{pen} = 40;

Here keys are head, book, pen and values are 10,20,40

%info  = (‘head’ => 10, ‘book’ => 20, ‘pen’ => 40);

print “$count{head}\n”;   it will give 10




Perl Loops

For repetitive execution of a segment of Perl code, loops are used. Loops keeps on repeating till certain conditions specified in the parameter are met.

Different Types of Loops:

–             For Loop

–             Foreach Loop

–             While Loop

–             Until Loop

–             Do..While Loop

–             Nested Loop

To know more on each types of loop above, Click Here

Control statements used inside Loops:

Goto statement:- It transfers one way control to another line of code.

Last statement:- It works like the ‘break’ statement in C program. It comes out of the innermost loop block and move on to the next available block in the code for execution.

Next statement:- It skips the execution of remaining block of code of same iteration without terminating loop and move cursor to next iteration.

Redo statement:- It lets control to jump to the beginning of the same loop

To read more on each of these type of statements, Click Here

Perl Operators

– Arithmetic:- +, -, *, /,**,%,-(unary negation)
– Comparison:- ==
– Assignment:- =
– Bitwise:- &,|,^

To read more on each type of these operators, Click Here

Read about File Handling in Perl

Read about Directory Handling in Perl