G C Reddy
Software Testing Complete Reference
PERL Script
PERL Script
Chapter-1 (Introduction)
Scripting Languages Vs Programming Languages
| Scripting Language | Programming Language |
| 1) It is an Interpreter based Language | 1) It is a compiler based Language. |
| 2) Interpreter converts high level instructions into machine language line by line | 2) Compiler converts the whole program in single short into machine language. |
| 3) It doesn’t 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
- 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.
- Perl is a high-level, general-purpose, interpreted, dynamic Scripting language.
- The home of perl is UNIX
- Perl Inherited features from UNIX utilities (awk, sed, grep, Smalltalk, Lisp, C, C++, Pascal and UNIX shell.
- Perl written in C language.
- Perl is free software.
- 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
§ 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”;
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
- It is a Data name or memory location name.
- It is used for to store data value.
- Value can change during execution of the program
- It is a temporary storage location.
- Perl allows implicit variable declaration.
- Every variable occupies memory at runtime.
Variables are classified into 3 types
- Scalar Variables
- Array Variables or List Variables
- Hash Variables or Associate Array Variables
1) Scalar Variables
- It holds one value.
- The value may be Integer or Float or String.
- 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
- It is a group of key pair values.
- 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 <>
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 “Don’t write to disk””; # wrong
$str= “I said |“Don’t write to disk|””; # right
$str= ‘I said “Don’t 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 “Don’t write to disk”|; # right
f) $x= “Unix/Linux”; # right
$x= qq|Unix/Linux|; # wrong
$x= qq|Unix/Linux|; # right
$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
STDIN – It used for to accept input from user
Ex: write a program accepting name and display?
Print “What is your name”;
$name=<STDIN>; 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= <STDIN>);
Print “Enter a number 2: “;
Chomp ($b= <STDIN>);
$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;
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;
$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
$x > $y;
output: false (0,0)
c) $a=100;
$b=20;
$c= $a<=>$b
if a>b output is 1
a<b output is -1
a==b output is 0
d) $x= “tecno”;
$y= “harika”;
$k=$x cmp $y
if x>y output is 1
x<y output is -1
x==y output is 0
3) Logical Operators:
&& (or) and
|| (or) or
! (or) not
4) Assignment Operators:
(=)
$a=10;
$b=20;
$c=30;
or
($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”;
$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)
{
Statements
——–
——–
——–
}
Statements
——–
——–
——–
2) Simple unless
unless (condition)
{
Statements
——–
——–
——–
}
Statements
——–
——–
——–
3) If….else
if (condition)
{
Statements
——–
——–
——–
}
else
{
Statements
——–
——–
——–
}
Statements
——–
——–
——–
4) unless….else
unless (condition)
{
Statements
——–
——–
——–
}
else
{
Statements
——–
——–
——–
}
Statements
——–
——–
——–
5) if….elseif…else
if (condition)
{
Statements
——–
——–
——–
}
else if (condition)
{
Statements
——–
——–
——–
}
else if (condition)
{
Statements
——–
——–
——–
}
else
{
Statements
——–
——–
——–
}
Statements
——–
——–
——–
6) Single line if statement
statement if (condition);
Statements
——–
——–
7) Single line unless statement
statement unless (condition);
Statements
——–
——–
b) Loop Statements:
1) While Loop
while (condition)
{
Statements
——–
——–
——–
}
Statements
——–
——–
——–
2) until Loop
until (condition)
{
Statements
——–
——–
——–
}
Statements
——–
——–
——–
3) Do while Loop
do
{
Statements
——–
——–
——–
}
while (condition)
{
Statements
——–
——–
——–
}
4) Do until Loop
do
{
Statements
——–
——–
——–
}
until (condition)
{
Statements
——–
——–
——–
}
5) For Loop
for (initiation; condition; increment/decrement)
{
Statements
——–
——–
——–
}
Statements
——–
——–
——–
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;
——–
——–
}
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?
Print “Enter a number: “;
Chomp ($n= <STDIN>);
If ($n >=100 && $a <=999)
{
print “$n is a 3 digit number”;
}
else
{
print $n is not a 3 digit number”;
2) Write a program accepting user name and password and check for given user name & password are valid or not?
Print “n Enter user name”;
Chomp ($uname= <STDIN>);
Print “n Enter password”;
Chomp ($pwd= <STDIN>);
If ($uname eq “tecno” && $pwd eq “soft”
{
print “n welcome to tecnosoft”;
}
else
{
print “n invalid user name or password”;
}
3) Write a program to print numbers 1 to 10?
$num=1;
while ($num ,=10)
{
print “$num n”;
$num++
}
(or)
for ($num=1; $num <= 10; $num++)
{
print “$num n”;
}
(or)
foreach $num(1..10)
{
print “$num n”;
}
(or)
foreach (1..10)
{
print “$_ n”;
}
(or)
foreach (1..10)
{
print ;
}
Note: $_ is Perl special and default variable. If we don’t declare any variable for reading data then Perl will store the value in default variable.
Perl Perl Perl Script
Tags: Perl, perl p1.pl, perl program compilation, perl script, perl testing

[...] PERL Script [...]
Thanks