Simple Perl Commands

by Geethalakshmi 2010-09-17 14:22:49

Simple Perl Commands


#!/usr/bin/perl
This command tells the operating system where the perl executable is located. The path may change from system to system. Use the whereis command in UNIX to locate it. This feature only works for UNIX based operating systems. In Windows, you may declare a file type of a particular extension and have it recognize the perl script in that manner. Otherwise, you must run the perl script using the perl command.

print
Prints list of arguments that it is passed.
print "Enter your name", "->";In this example, two strings are being passed to the print command. You could have just as easily used print "Enter your name ->";


The read function in perl is implemented using a < sign and a > sign. The variable in uppercase in the middle is referred to as the File Handle. A file handle connects the read function to an input stream. STDIN is the input stream associated with standard input. Standard input by default comes from your keyboard.
$name = ;This statement reads from standard input into a scalar variable named name.

chop
The chop function in perl is used to indiscriminately remove the last character from a variale.
chop($name);This statement removes the last character in the variable $name

Scalar Variables
Scalar variables in perl stored one piece of information that is not type specific meaning it could be a string or a number. They are created as you see in the example above using a simple assignment into a variable name preceded by the $.
print "My name is $name\n"; This command will print the words 'My name is' and the value of the variable $name

if
The if statement is used on a condition to change the flow of your program.
if($name eq "Patti")
{
print "It's Patti\n";
} else
{
print "It's someone else\n";
}
This command will print 'It's Patti' wherever the value stored in $name is Patti. Otherwise, it will print 'It's someone else'. The else statement is optional, but will only be executed when the condition in the if evaluates to false. If it evaluates to true, the block of code immediately following the if statement will be executed. The eq operator is the equality operator for string comparisons. Other, string comparison operators include lt, gt, le, ge

while
The while statement is used to iterate through some code time and time again.
while($num < 5)
{
print "The number is ", $num, "\n";
$num = $num + 1;
}
This command will print 'The number is' and num's value so long as the number is less than 5.

for
The for statement is also used to iterate through some code time and time again. Different from the while loop the number of iterations are known.
for($i = 0; $i < 5; $i++)
{
print "The number is ", $i, "\n";
}
This command will print 'The number is 0' all the way to 4. The for statement consists of the initialization of a counter variable, a test for that variable and a modification of the variable. As long as the condition is true, the loop will continue to iterate.

continue
The continue statement is used to stop the current iteration and go on to the next iteration
while($num < 5)
{
if($num % 2 == 1)
{
continue;
}
print "The number is ", $num, "\n";
$num = $num + 1;
}
This command will print 'The number is' and the even values so long as the number is less than 5. We are using the numerical comparison operators == and < and the modulus or remainder operator %. Other numeric operators include <=, >=, >, *, /, +, -

break
The break statement is used to jump out of a loop and execute the first line of code immediately following the loop
while($num < 5)
{
if($num % 5 == 0)
{
break;
}
print "The number is ", $num, "\n";
$num = $num + 1;
}
This command will print 'The number is' and the number so long as the number is less than 5 and not divisible by 5.

Note the semicolon is used as a separator of statements in perl and should be at the end of all your commands except for the if, while, and for statements.

Tagged in:

2399
like
0
dislike
0
mail
flag

You must LOGIN to add comments