Ads

Saturday, January 17, 2009

Introducing Perl,Part –IV

Loops in Perl

As in any other programming language, loops in Perl allow you to execute some set of statements over and over until some stop conditions are satisfied.

1. While loop: The while loop executes a set of statements repeatedly as long as a test expression in while loop evaluates to true. The test expression is evaluated before every iteration, so the block may get executed 0 times. There is also do..while, which is similar to while loop except that the condition is evaluated at the end. The following code prints the table of 2 using while loop.

#!/usr/bin/perl
$counter = 1;
While ($counter <= 10) {
printf (2x%d = %d\n”, $counter, 2*$counter);
$counter = $counter + 1;
}



2. Do...until loop: The do..until loop execute a set of statements repeatedly until the test expression in loop becomes true. The test expression is evaluated at the end of the loop. The following code print the table of 2.

#!/usr/bin/perl
$counter = 1;
do {
printf (“2x%d=%d\n”, $counter, 2*$counter);
$counter = $counter + 1;
} until ($counter > 9)


For loop: Perl also supports for loop. This has the following form:

for(initialization; test; inc)
{
statement1;
statement2;
statement3;
………….
………….
}


3. Foreach loop: perl uses the foreach loop for going through each line of an array or other list-like structures. This has the following form:

for $var (@array)
{
statement1;
statement2;
statement3;
………….
………….
}


Subroutines in Perl

A subroutine is a block of code that can be inserted anywhere in your program and executed by calling that subroutine. Subroutines have the following structure:

sub nameofsubroutine {
statement1;
statement1;
statement1;
…………..
…………..
}

Here sub is the keyword of Perl. You can call subroutine in the following manner:

$returnval = &nameof
Subroutine (arguments)

The return value is the value of the last executed statement in subroutine. The arguments given to the subroutine are passed on to the code inside the subroutine in the special array @_. You can access the first scalar argument with $_[0], the second with $_[0] and so on.


File handling in Perl

File handling is a basic feature in any programming language and perl has very powerful mechanisms for dealing with file input/output.

Opening a file: you can use the open function for opening a file. It has the following form:

open (FILEHANDLE, FILENAME);

FILEHANDLE are not denoted by special characters like @, $ and are not strings. The FILENAME is a string and could be specified in special formats shown below and open returns 1 on success and 0 on failure.

Special filename formats:

“FILE”: open FILE for input and is same as ““>FILE”: open FILE for output, create FILE if it does not exits
“>>FILE”: open FILE for appendind
“+>FILE”: open FILE with read/write access
“| CMD”: open a pipe to CMD
“CMD |”: open a pipe from CMD

Reading and writing to a file: for reading from a file, you can use < >operator. For example;

#for reading a line from the file
$line =
#for reading the entire file
@filecont =

You can use the print function for writing to a file. The print function takes an optional first arguments, which is a filehandle. The first argument is different from the list of scalars and there is no comma after the first argument.

Closing a file: To close a file use close function, which has the following form:

close (FILEHANDLE);



Key Features of Perl

Perl is a good choice for many developers. Here are some key features are given,

1. Web page design and CGI scripting
2. Prototyping
3. Auto-Code generation and
4. Information processing

Introducing Perl, Part-III

Perl expressions

Now, let’s see how to combine literals and variables using common operators to form expressions.
Numeric expressions and comparisons: Perl supports a number of numeric operators and functions, which take numbers as their arguments and return numbers as their results. The numeric operators are + for addition, - for subtraction, * for multiplication, / for division and ** for exponentiation. log, sin and sqrt are some numeric functions. And for numeric comparison, you can use <, <=, ==, !=, >= and > which have their usual meaning.
A combination of numeric data and operators is called a numeric expression.

String expressions and comparison: Basically there is only one string operators ‘.’, which is used to concatenate two strings. You can use le, lt, eq, ne, ge and gt for string comparison.

Boolean expressions and operators: often an expression’s result is important for whether it indicates truth or falsity. 0, 0.0, “”, “0”, and () mean false and anything other than these mean true. Common Boolean operators are ||, && and ! first two are binary operators ‘OR’ and ‘AND’ respectively. The ‘!’ is the not operator.

The ‘IF’ Statement

As in any other programming language, Perl also supports ‘if’ statements and its variations. The ‘if’ statement allows you to execute some set of statements dependent upon a condition being satisfied. For example,

if ($name eq ‘Euler’) {
print “Your name is Euler.”;
}

There can also be else conditions, e.g;

if ($name eq ‘Maan’) {
print “Your name is Maan”;
} elseif ($name eq ‘Sachin’) {
print “Your name is Sachin”;
} else {
print “You are not Maan nor sachin”;
}