0% found this document useful (0 votes)
17 views16 pages

WT - Unit - 3 (Except Last Topic)

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)
17 views16 pages

WT - Unit - 3 (Except Last Topic)

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/ 16

Module 3

Origin and Use of Perl

Perl Introduction
Perl is a programming language which was originally developed for script manipulation. But now
Perl is used for a variety of purpose including web development, GUI development, system
administration and many more. It is a stable, cross platform programming language.
For web development, Perl CGI is used. CGI is the gateway which interacts with the web browser
and Perl in a system.
Its typical use is extracting information from a text file and printing out report for converting a text
file into another form. This is because it got its name after the expression, "Practical Extraction and
Report Language".
Perl History

Perl was developed by Larry Wall in 1987 as a scripting language to make report processing easier.
It was first released with version 1.0 on December 18, 1987.
Perl 2, released in 1988 adding a much better regular expression engine.
Perl 3, released in 1989 adding support for binary data streams.
Perl Features

It has a very simple Object-oriented programming syntax.

 It is easily extendible as it supports 25,000 open source modules.


 It supports Unicode.
 It includes powerful tools to process text to make it compatible with mark-up languages like
HTML, XML.
 It supports third party database including Oracle, MySQL and many others.
 It is embeddable in other systems such as web servers and database servers.
 It is open source software licensed under GNU.
 Many frameworks are written in Perl.
 It can handle encrypted web data including e-commerce transactions.
 It is a cross platform language.
 It offers a regular expression engine which is able to transform any type of text.

Scalars and their Operations


Description:

A scalar contains a single unit of data. It is preceded with a ($) sign followed by letters, numbers
and underscores.
A scalar can contain anything a number, floating point number, a character or a string.
Two types of Scalars:

1) Numeric Scalar
2) String Scalar

1) Numberic Scalar

A scalar is most often either a number or a string.

Program:

$a=12;
$b=3.45;
$c=0xff;

print"integer value = $a \n";


print"float value = $b \n";
print"hexa to decimal value = $c \n";

Output:

integer value = 12
float value = 3.45
hexa to decimal value = 255

2) String Scalar

Program:

$a="welcome";
$b='keep silence';

print"1st output line = $a \n";


print"2nd output line = $b \n";

Output:

1st output line = welcome


2nd output line = keep silence

Scalar Operations in Perl Language

1) Multiline String
2) V-String
3) Special literals

1) Multiline String

Program 1:
print"welcome
to all my
students to
Perl Language
world \n";

Output:

welcome
to all my
students to
Perl Language
world

Program 2:

print<<EOF;
welcome
to all my
students to
Perl Language
world
EOF

Output:

welcome
to all my
students to
Perl Language
world

2) V-String:

A v-string provides an alternative and more readable way to construct strings, rather than use the
somewhat less readable interpolation form "\x{1}\x{14}\x{12c}\x{fa0}".

Program:

$a=v66.65.75.89;
print"String value = $a\n";

Output:

String value = BAKY

3) Special literals:

three special literals __FILE__, __LINE__, and __PACKAGE__ represent the current filename,
line number, and package name at that point in our program.
Program:

print"File name =" .__FILE__. "\n";


print"Line Number =" .__LINE__."\n";
print"Package name =" .__PACKAGE__."\n";

Output:
File name =test.pl
Line Number =2
Package name =main

Assignment Statements

Description:

Sr.No. Operator & Description


=

Simple assignment operator, Assigns values from right side operands to left side operand
1
Example − $c = $a + $b will assigned value of $a + $b into $c

+=

Add AND assignment operator, It adds right operand to the left operand and assign the
2 result to left operand

Example − $c += $a is equivalent to $c = $c + $a

-=

Subtract AND assignment operator, It subtracts right operand from the left operand and
3 assign the result to left operand

Example − $c -= $a is equivalent to $c = $c - $a

*=

Multiply AND assignment operator, It multiplies right operand with the left operand and
4 assign the result to left operand

Example − $c *= $a is equivalent to $c = $c * $a

/=

Divide AND assignment operator, It divides left operand with the right operand and assign
5 the result to left operand

Example − $c /= $a is equivalent to $c = $c / $a
%=

Modulus AND assignment operator, It takes modulus using two operands and assign the
6 result to left operand

Example − $c %= $a is equivalent to $c = $c % a

**=

Exponent AND assignment operator, Performs exponential (power) calculation on


7 operators and assign value to the left operand

Example − $c **= $a is equivalent to $c = $c ** $a

Program:

$a=12;
$a+=3;
print"a value = $a \n";

Output:

a value = 15

SIMPLE INPUT AND OUTPUT

Input Statements:

we can get input from standard console using <STDIN>. It stands for Standard Input. It can be
abbreviated by <>

1) <STDIN>

Program:

print"Enter your lucky number : \n";


$a=<STDIN>;
print"Your favourite number is = $a\n";

Output:

Enter your lucky number :


11
Your favourite number is = 11

Output Statements:

1) <STDOUT>
2) print
3) say
1) <STDOUT>

we can get output from standard console using <STDOUT>. It stands for Standard Input. It can be
abbreviated by <>

2) print:

print() function is one of the most commonly used function by the Perl users. It will print a string, a
number, a variable or anything it gets as its arguments.

program:

print"Enter your lucky number : \n";


$a=<STDIN>;
print"Your favourite number is = $a\n";

Output:

Enter your lucky number :


11
Your favourite number is = 11

3) say:

The say() function is not supported by the older perl versions. It acts like print() function with only
difference that it automatically adds a new line at the end without mentioning (\n).

program:

use 5.010;
say"i love Ice Cream";

Output:

i love Ice Cream

CONTROL STATEMENTS

1) if (if-only)
2) if-else
3) if-else-if

1) if (if-only):

The if statement is same as in other programming languages. It is used to perform basic condition
based task. It is used to decide whether a certain statement or block of statements will be executed
or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax:

if(condition)
{
# code to be executed
}

Program:

$a = 12;
if($a == 12)
{
print"a is 12 \n";
}

Output:

a is 12

2) if-else :

The if statement evaluates the code if the condition is true but what if the condition is not true, here
comes the else statement. It tells the code what to do when the if condition is false.

Syntax:

if(condition)
{
# code if condition is true
}
else
{
# code if condition is false
}

Program:

$a = 13;
if($a == 12)
{
print"a is 12 \n";
}
else
{
print"a is not 12 \n";
}

Output:

a is not 12

3) if-else-if

Here, a user can decide among multiple options. The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated with that get
executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else
statement will be executed.
Syntax:

if(condition1)
{
# code to be executed if condition1 is true
}
elsif(condition2)
{
# code to be executed if condition2 is true
}
elsif(condition3)
{
# code to be executed if condition3 is true
}
...
else
{
# code to be executed if all the conditions are false
}

Program:

$a = 13;
$b = 15;
$c = 17;
if(($a > $b) && ($a > $c))
{
print"a is big \n";
}
elsif($b > $c)
{
print"b is big \n";
}
else
{
print"c is big \n";
}

Output:

c is big

Looping Statements

1) forloop
2) while loop
3) do-while loop

1) for loop statement:

program:

for($i=0; $i<=5;$i++)
{
print"$i \n";
}

Output:

0
1
2
3
4
5

2) while-loop statement:

Program:

$i=0;
while($i<=5)
{
print"$i \n";
$i++;
}

Output:

0
1
2
3
4
5

3) do-while loop statement

Program:

$i=0;
do
{
print"$i \n";
$i++;
}while($i<=4)

Output:

0
1
2
3
4
FUNDAMENTALS OF ARRAYS

Description:

Array is a special type of variable. The array is used to store the list of values and each object of the
list is termed as an element. Elements can either be a number, string, or any type of scalar data
including another variable.

Program :

@arr = (10...15);

$a = @arr;
print"Array length = $a\n";

$b = $#arr;
print"Array index = $b\n";

print"Array Values:\n";
foreach $a (@arr)
{
print"$a \n";
}

Output:

Array length = 6
Array index = 5
Array Values:
10
11
12
13
14
15

HASHES

Description:

A hash is a set of key-value pairs. Perl stores elements of a hash such that it searches for the values
based on its keys. Hash variables start with a ‘%’ sign.
Perl requires the keys of a hash to be strings, whereas the values can be any scalars. These values
can either be a number, string or reference. If non-string values are used as the keys, it gives an
unexpected result.
Program 1:

%xyz = ('a',10,'b',20,'c',30,'d',40);
@arr = keys %xyz;
foreach $a (@arr)
{
print"$a \n";
}

Output:
c
b
a
d

Program 2:

%xyz = ('a',10,'b',20,'c',30,'d',40);

@arr = values %xyz;


foreach $a (@arr)
{
print"$a \n";
}

Output:

20
30
40
10

REFERENCES
Description:

A Perl reference is a scalar data type that holds the location of another value which could be scalar,
arrays, or hashes. Because of its scalar nature, a reference can be used anywhere, a scalar can be
used.

Program:

%xyz = ('a',10,'b',20,'c',30,'d',40);
$a=\%xyz;
print"Hash Address = $a \n";

Output:

Hash Address = HASH(0x9229a30)

FUNCTIONS
Description:

A Perl subroutine or function is a group of statements that together performs a task. You can divide
up your code into separate subroutines. How you divide up your code among different subroutines
is up to you, but logically the division usually is so each function performs a specific task.
Classifications:

1) Simple function program


2) Local anf global variable
3) Function with hashes
4) Function with arrays

1) Simple function program:

Program:

function();
sub function
{
print"welcome foods\n";
}

Output:

welcome foods

2) Local anf global variable

By default, all the variables are global variables inside Perl. But you can create local or private
variables inside a function with 'my' keyword.
The 'my' keyword restricts the variable to a particular region of code in which it can be used and
accessed. Outside this region, this variable can not be used.

Program:

$a = "Mutton Gravy";

function();
sub function
{
my $a = "Chicken Gravy";
print"$a \n";
}

print"$a \n";

Output:

Chicken Gravy
Mutton Gravy

3) Function with hashes:

Program:
%abc = ('a',10,'b',20,'c',30,'d',40);
function(%abc);
sub function
{
%abc= @_;
foreach $x (keys %abc)
{
$value = %abc{$x};
print"$x = $value \n";
}}

Output:

a = 10
d = 40
c = 30
b = 20

4) Function with arrays:

Program:

@arr = qw/Idly Dosa Boori Noodules/;


function(@arr);
sub function
{
foreach $a (@arr)
{
print"$a \n ";
}}

Output:

Idly
Dosa
Boori
Noodules

PATTERN MATCHING
Description:

Perl matching operators have various modifiers. It is mainly used to match a string or statement to a
regular expression.

Perl Matching Operator =~


The matching operator =~ is used to match a word in the given string. It is case sensitive, means if
string has a lowercase letter and you are searching for an uppercase letter then it will not match.
Program 1:
$line = qw/welcome to food junction /;

if($line = ~/junction/)
{
print"Matching \n";
}
else
{
print"Not Matching \n";
}

Output:

Matching

FILE INPUT AND OUTPUT


Description:

A filehandle is a named internal Perl structure that associates a physical file with a name. All
filehandles are capable of read/write access, so you can read from and update any file or device
associated with a filehandle.

Following is the table, which gives the possible values of different modes

Sr.No. Entities & Definition


< or r
1
Read Only Access

> or w
2
Creates, Writes, and Truncates

>> or a
3
Writes, Appends, and Creates

+< or r+
4
Reads and Writes

+> or w+
5
Reads, Writes, Creates, and Truncates

+>> or a+
6
Reads, Writes, Appends, and Creates
File Operations:

1) Create the file


2) Write the file
3) Read the file
4) close the file
5) Rename the file
6) Copy the file
7) Remove the file

1) Create the file

Program:

open(DATA, ">file3.txt") or die "file not created";


print"file created successfully \n";

Output:

file created successfully

2) Write the file

Program:

open(DATA, ">file3.txt") or die "file not write";


print DATA "welcome morning";
print"file write successfully \n";

Output:

file write successfully

3) Read the file

Program:

open(DATA, "<file3.txt") or die "file not read";


print readline(DATA);

Output:

welcome morning

4) Close the file

Program:

open(DATA, ">file3.txt") or die "file not close";


print "file closed successfully\n";
Output:
file closed successfully

5) Rename the file

Program:

rename("file3.txt", "file4.txt");
print"file renamed successfully\n";

Output:

file renamed successfully

6) Copy the file

Program:

open (DATA, "<file4.txt") or die "file can not read";

open (DATA2, ">file5.txt") or die "file can not create";

print DATA2 readline(DATA);

print "file copied successfully \n";

Output:

file copied successfully

7) Remove the file

Program:

unlink("file4.txt");
print"file removed successfully\n";

Output:

file removed successfully

You might also like