WT - Unit - 3 (Except Last Topic)
WT - Unit - 3 (Except Last Topic)
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
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
Program:
$a=12;
$b=3.45;
$c=0xff;
Output:
integer value = 12
float value = 3.45
hexa to decimal value = 255
2) String Scalar
Program:
$a="welcome";
$b='keep silence';
Output:
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:
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:
Output:
File name =test.pl
Line Number =2
Package name =main
Assignment Statements
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
**=
Program:
$a=12;
$a+=3;
print"a value = $a \n";
Output:
a value = 15
Input Statements:
we can get input from standard console using <STDIN>. It stands for Standard Input. It can be
abbreviated by <>
1) <STDIN>
Program:
Output:
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:
Output:
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:
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
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
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);
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:
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:
Program:
function();
sub function
{
print"welcome foods\n";
}
Output:
welcome foods
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
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
Program:
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.
if($line = ~/junction/)
{
print"Matching \n";
}
else
{
print"Not Matching \n";
}
Output:
Matching
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
> 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:
Program:
Output:
Program:
Output:
Program:
Output:
welcome morning
Program:
Program:
rename("file3.txt", "file4.txt");
print"file renamed successfully\n";
Output:
Program:
Output:
Program:
unlink("file4.txt");
print"file removed successfully\n";
Output: