8/6/2019 Gc Reddy Perl Scripting
1/24
PERL Script
Posted by admin in QTP Training on 10 4th, 2010 | 2 responses
PERL Script
Chapter-1 (Introduction)
Scripting Languages VsProgrammingLanguages
Scripting Language Programming Language
1) It is an Interpreter based Language 1) It is a compiler based Language.
2) Interpreter converts highlevelinstructions into machine
language line by line
2) Compiler converts the whole program in singleshort into machine language.
3) It doesnt create executable file. 3) It Creates .exe file.4) No need to compile the program Need to compile the program
5) It takes less code 4) It takes numerous lines of code
6) It greatly reduces development time 5) It increases development time
7) It reduces maintenance of cost 7) It Increases maintenance of cost
PERL-Practical Extraction and Reporting Language
y Perl was originally developed by Larry Wall, a linguist working as a systems administrator for NASA,in 1987, as a general-purpose Unix scripting language to make report processing easier.
y Perl is a high-level, general-purpose, interpreted, dynamic Scripting language.y The home of perl is UNIXy Perl Inherited features from UNIX utilities (awk, sed, grep, Smalltalk, Lisp, C, C++, Pascal and UNIX
shell.
y Perl written in C language.y Perl is free software.y Perl is open source code.
o Perl is highly portable Language (supports 76+ Operating systems, Scripts developed in one
operating system allows to run on other operating system with/without modifications.)
o Perl supports oops concepts known as object oriented perl.
o Perl supports database connectivity.
o Perl has given CPAN (Comprehensive Perl Archive Network) modules.website is www.cpan.org
o Perl was originally developed for to do manipulations in the text files later it is used in wide range of
the following areas
System Administration
8/6/2019 Gc Reddy Perl Scripting
2/24
Database Administration
Network Programming
Web Development
Software Testing
Hardware Testing
Telecom
Vlsi
Baoinformatics and so on
o It is built-in with UNIX and Its flavers like Linux, Solaris etc
o Perl is a case sensitive Language.
o Each and every statement should end with (;) semicolon.
o Perl is an interpreter based Language.
o Extension of perl program is .pl or .plx
o Perl supports two types of comments.
Single line comment (#)
Multi line comment
Pod (perl old document)
Cut
Perl program compilation Process:
Writing a program to display welcome message
#! C:perlbinperl
print welcome to Perl n;
8/6/2019 Gc Reddy Perl Scripting
3/24
print This is my first program;
Save with first.pl
Running perl Program in Windows
C:perl first.pl
#! It is shebang statement, used for to invoke perl interpreter path.
Running perl Program in UNIX/Linux
$ which perl # it displays location of perl
usr/bin/perl
perl v # version of perl
$ man perl # help document
$ vi first.pl
#! usr/bin/perl
print welcome to Perl n;
print This is my first program;
$ perl first.pl
or
$ chmod 755 first.pl
$./first.pl
Chapter-2 (Variables)
Variables
y It is a Data name or memory location name.y It is used for to store data value.y Value can change during execution of the programy It is a temporary storage location.y Perl allows implicit variable declaration.y Every variable occupies memory at runtime.
8/6/2019 Gc Reddy Perl Scripting
4/24
Variables are classified into 3 types
y Scalar Variablesy Array Variables or List Variablesy Hash Variables or Associate Array Variables
1) Scalar Variables
y It holds one value.y The value may be Integer or Float or String.y The Variable should begin with $ symbol.
a=10; wrong
$a=10; right
2) Array Variables
o It is a group of scalar values
o It should begin with @ symbol.
3) Hash Array Variables
y It is a group of key pair values.y It should begin with % symbol.
1) Scalar Variables
$a=10; # Integer
$b=1.5; # Float
$c=Gcreddy #String
$d=perl is a scripting language # String
$e=100 # String
$f=Perl # String
Strings are classified into 3 types
1) Double quoted strings ( ) or qq with delimiter of any one || or [] or {} or
2) Single quoted strings ( ) or q with delimiter of any one || or [] or {} or
8/6/2019 Gc Reddy Perl Scripting
5/24
3) Back tick / quoted strings (` `) or qx with delimiter of any one || or [] or {} or
a) $str=perl;
$x= I like $str;
print $x;
Output: I like Perl
b) $x= I like $str;
print $x
Output: I like $str
c) $x= I like perl n $str
print $x;
output: I like
Perl
d) $x= I like n $str;
print $x;
output: I like $x;
e) $str= I said Dont write to disk; # wrong
$str= I said |Dont write to disk|; # right
$str= I said Dont write to disk; # wrong
$str= I said Don|t write to disk; # right
$str= qq| I said Don|t write to disk|; # right
$str= q|I said Dont write to disk|; # right
f) $x= Unix/Linux; # right
$x= qq|Unix/Linux|; # wrong
$x= qq|Unix/Linux|; # right
8/6/2019 Gc Reddy Perl Scripting
6/24
$x= qq{Unix/Linux}; # wrong
note: qq for variable substitute
q for as it is
qx for OS command
# $x= `dir`;
`md xyz`;
$x= qx|dir|;
print $x;
perl c pl.pl # compilation ok
Writing output:
Perl p1.pl>a1 # over write
Perl p1.pl>>a2 # add
Perl p1.pl> D:a3 # path
Standard Input / Output Handlers
print () it used for to write data to the screen
print(Hello);
or
print Hello;
or
print STDOUT Hello;
I/O handlers
1) STDIN
2) STDOUT
3) STDERR
8/6/2019 Gc Reddy Perl Scripting
7/24
STDIN It used for to accept input from user
Ex: write a program accepting name and display?
Print What is your name;
$name=; or # Diamond operator
print Hello $name, Good morning;
chmod ($name); # It is a pre-defined function, it deletes the given string lost line character if it is new line.
Ex2: write a program accept 2 integer values and find sum?
Print Enter a number 1: ;
Chomp ($a= );
Print Enter a number 2: ;
Chomp ($b= );
$c= $a + $b
print n $a + $b= $c;
Chapter-3 (Operators)
1) Arithmetic Operators:
+, -, *, /, %, ** (right to left)
a) $a=10;
$b=20;
print $a + $b;
output: 30
b) $a=10;
$b=25abc;
print $a + $b;
8/6/2019 Gc Reddy Perl Scripting
8/24
output: 35
a) $a=10;
$b=abc20;
print $a + $b;
output: 10
d) $a=10;
$b=20abc34;
print $a + $b;
output: 30
e) $a=perl;
$b=gcreddy;
print $a + $b;
output: 0
f) $a=10;
$b=25abc;
print $a + $b;
output: 35
print $a .$b;
output: 1025abc
g) $a=2;
$b=3;
print $a ** $b;
output: 8
h) $a=2;
8/6/2019 Gc Reddy Perl Scripting
9/24
$b=3;
$c=2;
print $a ** $b ** $c;
output: 512
i) $a=2;
$b=3;
$c=2;
print ($a ** $b) ** $c;
output: 64
2) Relational Operators:
i) Numeric Comparison Operators
, =, ==, !=,
ii)String Comparison Operators
lt, gt, le, ge, eq, ne, cmp
a) $a=100;
$b=20;
$a>$b;
output: true
$a gt $b;
output: false (ansii nos)
b) $x= tecno;
$y= harika;
$x gt $y;
output: true
8/6/2019 Gc Reddy Perl Scripting
10/24
$x > $y;
output: false (0,0)
c) $a=100;
$b=20;
$c= $a$b
if a>b output is 1
ay output is 1
x
8/6/2019 Gc Reddy Perl Scripting
11/24
($a, $b, $c) = (10,20,30);
5) String Multiplication Operators:
(x)
Ex: $str=perl;
$k= $str x 5;
print $k;
output: perl perl perl perl perl
ex2: print -; x 50
output: -
ex3: print _; x 50
output: _________________________________________________
6) Range Operators:
(..)
1..10 # 12345678910
a..z #abcdefghijklmnopqrstuvwxyz
h..t # hijklmnopqrst
-10..1 #-10-9-8-7-6-5-4-3-2-101
10..1 # wrong
-1..-10 # wrong
z..a # wrong
7) String concatenation Operators:
(.)
It is used for to join two or more strings
$str= Load;
8/6/2019 Gc Reddy Perl Scripting
12/24
$str=$str. ing;
Short hand Assignment Operators:
+=, -=, *=, /=, %=, .=, x=
$a-10;
$a= $a+5;
(or)
$a+=5;
9) Conditional Operators or Ternary operators:
(?:)
expression 1 ? expression 2 : expression 3
$a=100;
$b=99;
$a > $b ? print $a is big : print $b is big;
10) Incremental Operator (++)
$a=10;
$a=$a+1; or $a+=1; or $a++;
11) Detrimental Operators ()
$a=$a-1; or $a-=1 or $a;
-****-****
Chapter-4 (Control flow statements)a) Conditional Statements:
1) Simple if condition
if (condition)
8/6/2019 Gc Reddy Perl Scripting
13/24
{
Statements
}
Statements
2) Simple unless
unless (condition)
{
Statements
}
Statements
3) If.else
if (condition)
8/6/2019 Gc Reddy Perl Scripting
14/24
{
Statements
}
else
{
Statements
}
Statements
4) unless.else
unless (condition)
{
Statements
8/6/2019 Gc Reddy Perl Scripting
15/24
}
else
{
Statements
}
Statements
5) if.elseifelse
if (condition)
{
Statements
}
else if (condition)
{
Statements
8/6/2019 Gc Reddy Perl Scripting
16/24
}
else if (condition)
{
Statements
}
else
{
Statements
}
Statements
6) Single line if statement
statement if (condition);
Statements
8/6/2019 Gc Reddy Perl Scripting
17/24
7) Single line unless statement
statement unless (condition);
Statements
b) Loop Statements:
1)While Loop
while (condition)
{
Statements
}
Statements
2) until Loop
until (condition)
{
8/6/2019 Gc Reddy Perl Scripting
18/24
Statements
}
Statements
3) Dowhile Loop
do
{
Statements
}
while (condition)
{
Statements
}
8/6/2019 Gc Reddy Perl Scripting
19/24
4) Do until Loop
do
{
Statements
}
until (condition)
{
Statements
}
5) For Loop
for (initiation; condition; increment/decrement)
{
Statements
}
Statements
8/6/2019 Gc Reddy Perl Scripting
20/24
6) For Each Loop
foreach variable (list of variables)
{
Statements
}
Statements
7) Last keyword:
It is used for to terminate the loop; it is same as break in C Language.
While (condition)
{
Statements
last;
8/6/2019 Gc Reddy Perl Scripting
21/24
}
Statements
Next keyword:
It is a keyword, used for to place control at beginning of the loop. It is same as continue in C language.
While (condition)
{
Statements
next;
}
Statements
Examples:
1) Write a program accepting a number and check given number is 3 digit number or not?
8/6/2019 Gc Reddy Perl Scripting
22/24
Print Enter a number: ;
Chomp ($n= );
If ($n >=100 && $a
8/6/2019 Gc Reddy Perl Scripting
23/24
$num=1;
while ($num ,=10)
{
print $num n;
$num++
}
(or)
for ($num=1; $num
8/6/2019 Gc Reddy Perl Scripting
24/24
print ;
}
Note: $_ is Perl special and default variable. If we dont declare any variable for reading data then Perl will
store the value in default variable.
Perl Perl Perl Script
Top Related