5 Perl
5 Perl
What is PERL?
Speed of development
You can enter the program in a text file, and just run it. It is an
interpretive language; no compiler is needed.
It is powerful
The regular expressions of Perl are extremely powerful.
Portability
Perl is a standard language and is available on all platforms.
Free versions are available on the internet.
Flexibility
Perl does not limit the size of your data
If memory is available, Perl can handle the whole file as a single
string
Allows one to write simple programs to perform complex tasks
How to run Perl?
Assumptions:
For Windows operating system, you are running Perl programs
from the command prompt.
Run "cmd" to get command prompt window
Recommended steps
Create a directory/folder where you will be storing the Perl
files.
Using any text editor, create a file "test.pl" with the following
content:
#!/usr/bin/perl
print “Hello World!\n”;
First Perl Program
# welcome.pl
print ( "1. Welcome to Perl!\n" );
print "2. Welcome to Perl!\n" ;
print "3. Welcome ", "to ", "Perl!\n";
print "4. Welcome ";
print "to Perl!\n";
print "5. Welcome to Perl!\n";
print "6. Welcome\n to\n\n Perl!\n";
1. Welcome to Perl!
2. Welcome to Perl!
3. Welcome to Perl!
4. Welcome to Perl!
5. Welcome to Perl!
6. Welcome
to
Perl!
Cont..
=begin comment
This is all part of multiline comment.
You can use as many lines as you like
These comments will be ignored by the
compiler until the next =cut is encountered.
=cut
O/P:
O/P:
Hello
Hello, world
world
Hello, world\n
$a = 10;
print "Value of a = $a\n";
print 'Value of a = $a\n';
O/P:
Value of a = 10
Value of a = $a\n
Cont..
$a = 10;
$var = <<"EOF";
This is the syntax for here document and it will
continue
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
O/P:
interpolated. For example value of a = $a
EOF
This is the syntax for here document and it will
continue
print "$var\n";
until it encounters a EOF in the first line.
This is case of double quote so variable value will be
$var = <<'EOF';
interpolated. For example value of a = 10
This is case of single quote so variable value will not
be
This is case of single quote so variable value will not
interpolated. For example value of a = $a
be
EOF
interpolated. For example value of a = $a
print "$var\n";
Cont..
O/P:
This is "number"
$result
Data Type
Arrays of scalars
2 Arrays − Arrays are ordered lists of scalars that you access with a
numeric index which starts with 0. They are preceded by an "at"
sign (@).
O/P:
Age = 25
Name = John Paul
Salary = 1445.5
Cont..
O/P:
$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = John Paul
$names[1] = Lisa
$names[2] = Kumar
Cont..
O/P:
Given names are : John Paul Lisa Kumar
Number of names are : 3
Cont..
O/P:
str = helloworld
num = 15
mix = helloworld15
mul = 20
Cont..
O/P:
Mon
Tue
Wed
Sun
Sun
Mon
Cont..
@var_10 = (1..10);
@var_20 = (10..20);
@var_abc = (a..z);
O/P:
1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20
abcdefghijklmnopqrstuvwxyz
Cont..
@array = (1,2,3);
print "Size: ",scalar @array,"\n";
O/P:
Size: 3 @array = (1,2,3);
$array[50] = 4;
$size = @array;
$max_index = $#array;
O/P:
Size: 51
Max Index: 50
Cont..
@weekdays = @days[3,4,5];
print "@weekdays\n";
O/P:
Thu Fri Sat
@weekdays = @days[3..5];
print "@weekdays\n";
O/P:
Thu Fri Sat
Cont..
O/P:
@odd = (1,3,5);
Before: pizza steak chicken burgers
@even = (2, 4, 6);
After: burgers chicken pizza steak
@numbers = (@odd, @even);
O/P:
$data{'John Paul'} = 45
$data{'Lisa'} = 30
$data{'Kumar'} = 40
Loops
$a = 10;
# while loop execution # for loop execution
while( $a < 20 ) for( $a = 10; $a < 20; $a = $a + 1 )
{ {
print"Value of a: $a\n"; print "value of a: $a\n";
$a = $a + 1; }
}
O/P:
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19
Subroutines
# Function definition
sub Hello{
print "Hello, World!\n";
}
# Function call
Hello(); O/P: Hello, World!
Cont..
# Function definition
sub Average{
# get total number of arguments passed.
$n = scalar(@_);
$sum = 0;
# Function call
Average(10, 20, 30); O/P: Average for the given numbers : 20
Thank You..