0% found this document useful (0 votes)
68 views32 pages

5 Perl

Perl is an interpreted programming language used for text manipulation and reporting. It was created by Larry Wall and is available on all platforms. Perl is useful for scanning text files, extracting information, and generating reports. It allows for rapid development through scripting. Key features include powerful pattern matching, portability, flexibility with large data, and ease of use with simple text editors. Perl code is run by saving files with a .pl extension and executing them from the command line. Basic data types in Perl include scalars, arrays, and hashes.

Uploaded by

Muskan agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views32 pages

5 Perl

Perl is an interpreted programming language used for text manipulation and reporting. It was created by Larry Wall and is available on all platforms. Perl is useful for scanning text files, extracting information, and generating reports. It allows for rapid development through scripting. Key features include powerful pattern matching, portability, flexibility with large data, and ease of use with simple text editors. Perl code is run by saving files with a .pl extension and executing them from the command line. Basic data types in Perl include scalars, arrays, and hashes.

Uploaded by

Muskan agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Introduction to PERL

What is PERL?

 Practical Extraction and Report Language.


 Developed for text manipulation.
 PERL was created by Larry Wall.
 It is an interpreted language optimized for scanning
arbitrary text files, extraction information from them
and printing reports based on that information.
 Available on all platform.
 Interpreter programming.
Cont..

 A scripting language which is both relatively simple


to learn.
 Perl is an Open Source software.
 Perl is a case sensitive programming language. Thus
$Manpower and $manpower are two different
identifiers in Perl.
Advantages

 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.

 Uses sophisticated pattern matching techniques to scan large


amounts of data very quickly
Cont..

 Portability
 Perl is a standard language and is available on all platforms.
 Free versions are available on the internet.

 Editing Perl programs


 No sophisticated editing tool is needed
 Any simple text editor like notepad or vi will do

 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?

 Perl can be downloaded from the internet.


 Available on almost all platforms

 Assumptions:
 For Windows operating system, you are running Perl programs
from the command prompt.
 Run "cmd" to get command prompt window

 For Unix/Linux, you are running directly from the shell


prompt
Working through an example

 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:

print “Hello World!\n”;

 Execute the program by typing the following at the command


prompt:
perl test.pl
Cont..

 On Unix/Linx, an additional line has to be given at


the beginning of every Perl program

#!/usr/bin/perl
print “Hello World!\n”;
First Perl Program

 print “Hello World!\n”;

 This is a Perl ‘statement’, or line of code


 ‘print’ is a function - one of many
 “Hello World!\n” is a string of characters
 note the ‘\n’ is read as a single character meaning ‘newline’
 The semicolon ‘;’ tells the interpreter that this line of code is
complete.
Cont..

# 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..

 A Perl file must be saved with a .pl or .PL file


extension
 A line starting with hash #is a comment.
 Lines starting with = are interpreted as the start of a
section of embedded documentation (pod), and all
subsequent lines until the next =cut are ignored by
the compiler.
Cont..

print "Hello, world\n";

=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: Hello, world


Sample Programs

print "Hello, world\n";


print "Hello
print 'Hello, world\n';
world\n";

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..

$result = "This is \"number\"";


print "$result\n";
print "\$result\n";

O/P:
This is "number"
$result
Data Type

 Perl is a loosely typed language and there is no need


to specify a type for your data while using in your
program. The Perl interpreter will choose the type
based on the context of the data itself.

 Perl has three basic data types:


 Scalars

 Arrays of scalars

 Hashes of scalars, also known as associative arrays.


Cont..

S. No. Types and Description


1 Scalar − Scalars are simple variables. They are preceded by a
dollar sign ($). A scalar is either a number, a string, or a
reference. A reference is actually an address of a variable, which
we will see in the upcoming chapters.

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 (@).

3 Hashes − Hashes are unordered sets of key/value pairs that you


access using the keys as subscripts. They are preceded by a
percent sign (%).
Cont..

$age = 25; # An integer assignment


$name = "John Paul"; # A string
$salary = 1445.50; # A floating point

print "Age = $age\n";


print "Name = $name\n";
print "Salary = $salary\n";

O/P:
Age = 25
Name = John Paul
Salary = 1445.5
Cont..

@ages = (25, 30, 40);


@names = ("John Paul", "Lisa", "Kumar");

print "\$ages[0] = $ages[0]\n";


print "\$ages[1] = $ages[1]\n";
print "\$ages[2] = $ages[2]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n";

O/P:
$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = John Paul
$names[1] = Lisa
$names[2] = Kumar
Cont..

%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);

print "\$data{'John Paul'} = $data{'John Paul'}\n";


print "\$data{'Lisa'} = $data{'Lisa'}\n";
print "\$data{'Kumar'} = $data{'Kumar'}\n";
@names = ('John Paul', 'Lisa', 'Kumar');
@copy = @names;
O/P:
$size = @names;
$data{'John Paul'} = 45
$data{'Lisa'} = 30
print "Given names are : @copy\n";
$data{'Kumar'} = 40
print "Number of names are : $size\n";

O/P:
Given names are : John Paul Lisa Kumar
Number of names are : 3
Cont..

$str = "hello" . "world"; # Concatenates strings.


$num = 5 + 10; # adds two numbers.
$mul = 4 * 5; # multiplies two numbers.
$mix = $str . $num; # concatenates string and
number.

print "str = $str\n";


print "num = $num\n";
print "mix = $mix\n";
print "mul = $mul\n";

O/P:
str = helloworld
num = 15
mix = helloworld15
mul = 20
Cont..

@days = qw/Mon Tue Wed Thu Fri Sat Sun/;


print "$days[0]\n";
print "$days[1]\n";
print "$days[2]\n";
print "$days[6]\n";
print "$days[-1]\n";
print "$days[-7]\n";

O/P:
Mon
Tue
Wed
Sun
Sun
Mon
Cont..

@var_10 = (1..10);
@var_20 = (10..20);
@var_abc = (a..z);

print "@var_10\n"; # Prints number from 1 to 10


print "@var_20\n"; # Prints number from 10 to 20
print "@var_abc\n"; # Prints number from a to 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;

print "Size: $size\n";


print "Max Index: $max_index\n";

O/P:
Size: 51
Max Index: 50
Cont..

# create a simple array


@coins = ("Quarter","Dime","Nickel");
print "1. \@coins = @coins\n";

# add one element at the end of the array


push(@coins, "Penny");
print "2. \@coins = @coins\n";

# add one element at the beginning of the array


unshift(@coins, "Dollar"); O/P:
print "3. \@coins = @coins\n"; 1. @coins = Quarter Dime Nickel
2. @coins = Quarter Dime Nickel Penny
3. @coins
# remove one element from the last = Dollar Quarter Dime Nickel Penny
of the array.
pop(@coins); 4. @coins = Dollar Quarter Dime Nickel
print "4. \@coins = @coins\n"; 5. @coins = Quarter Dime Nickel

# remove one element from the beginning of the array.


shift(@coins);
print "5. \@coins = @coins\n";
Cont..

@days = qw/Mon Tue Wed Thu Fri Sat Sun/;

@weekdays = @days[3,4,5];

print "@weekdays\n";

O/P:
Thu Fri Sat

@days = qw/Mon Tue Wed Thu Fri Sat Sun/;

@weekdays = @days[3..5];

print "@weekdays\n";

O/P:
Thu Fri Sat
Cont..

@foods = qw(pizza steak chicken burgers);


print "Before: @foods\n";

# sort this array


@foods = sort(@foods);
print "After: @foods\n";

O/P:
@odd = (1,3,5);
Before: pizza steak chicken burgers
@even = (2, 4, 6);
After: burgers chicken pizza steak
@numbers = (@odd, @even);

print "numbers = @numbers\n";


O/P:
numbers = 1 3 5 2 4 6
Cont..

%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);

print "\$data{'John Paul'} = $data{'John


Paul'}\n";
print "\$data{'Lisa'} = $data{'Lisa'}\n";
print "\$data{'Kumar'} = $data{'Kumar'}\n";

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

A Perl subroutine or function is a group of


statements that together performs a task.

# 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;

foreach $item (@_){


$sum += $item;
}
$average = $sum / $n;

print "Average for the given numbers : $average\n";


}

# Function call
Average(10, 20, 30); O/P: Average for the given numbers : 20
Thank You..

You might also like