SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Learn PERL

PERL - Practical Extraction and Reporting Language

1.

Where do we use PERL: ........................................................................................................................ 2

2.

What are the key features of PERL: ...................................................................................................... 2

3.

SCALAR Data: ........................................................................................................................................ 2

4.

USER IO: ................................................................................................................................................ 5

5.

STRING OPERATIONS: ........................................................................................................................... 5

6.

CHOMP: ................................................................................................................................................. 6

7.

LOOPS and CONDITIONS: ...................................................................................................................... 6

8.

ARRAYS: ............................................................................................................................................... 11

9.

Usage of $_ ......................................................................................................................................... 13

10.

Sub Routines: .................................................................................................................................. 16

11.

PERL DB ........................................................................................................................................... 17

12.

PERL FILE HANDLING ....................................................................................................................... 18

13.

PERL REGULAR EXPRESSION ........................................................................................................... 20

14.

PERL HASH....................................................................................................................................... 20

Praveen Kasireddy

Page 1
Learn PERL

1. Where do we use PERL:
Quick and dirty programs (automating simple daily jobs)
System Administration
Web backend (CGI/Database access)
Configuration Management
QA
Biotechnology
Prototypes

2. What are the key features of PERL:
Text processing (pattern matching)
List processing
Database access
System language

To know if PERL is installed in your machine, you can try perl –v or perl –V
Sample PERL code: perl -e "print 1000" will print à1000
# is a comment.
perldoc provides documentation about any one command
3. SCALAR Data:
A single piece of data either a number or a string is called a 'scalar' in Perl.
PERL Takes care of data types on its own:
print 3+4;

#7

print 9-7;

#2

print 2*3;

#6

print 3/0;

# ERROR - Even Perl cannot divide by 0

Print 11 % 3; # 2 (modulus)

Praveen Kasireddy

Page 2
Learn PERL

print 2 ** 3; # 8 (power of)
print "another 'string'"; # another 'string'
print "escape" this"; # ERROR
print "escape" this"; # escape" this
STRING Operators:
print "a" . "b"; # ab - concatenation
print "e" x 3; # eee - repetition
print "perl " x 3; # perl perl perl
print "-" x 80; # ------- (80 times)
SCALAR Variable declaration:
To save the values for later use in your program you need variables.
Rules:




Start with letter or underscore
Contains letters, underscores and digits
Case sensitive

** Same scalar variable can store integers and strings:
$x = 3;
$y = $x + 2;
$z = "hello world";
$z = 8;
String TO Number Conversion:
$x = "3 apples";
$y = "5 banana";
Praveen Kasireddy

Page 3
Learn PERL

$total = $x + $y;
print $total; # 8
# Using a numerical operator forces the strings to be numbers
print "3a2" * 4; # 12
print "7683a2" * 1; # 7683
print "a2" * 4; # 0
print "010a2" + 4; # 14
print "3.1" + 0; # 3.1
print "3e+2x" + 0; # 300
Special Observation:
print "2" + 3; # 5 - (+) is a numerical operator
print "2" . 3; # 23 - (.) is a string operator
Variable Interpolation:
$c = 3;
$what = "apples";
$q = "$c $what";
print $q; # 3 apples
works in double quoted strings only
print '$c $what'; # $c $what
Variable name
$what = "banana";
$c = 3;
print "$c $what"; # 3 banana
Praveen Kasireddy

Page 4
Learn PERL

print "$c $whats"; # 3 (warning)
print "$c ${what}sn"; # this works well
$c = 3;
$what = 'apple';
print "$c $what sn";
print $c . " " . $what . " sn";
print $c, " ", $what , " sn";

# all will result in '3 apple s' followed by a newline

4. USER IO:
$line = <STDIN>; # To Accept User inputs
if ($line eq "") {
print "Blank line";
}
print $line;

5. STRING OPERATIONS:
substr returns a substring of a big string
$part = substr($big, $index_of_first, $length);
Examples:
$big = "This is a big string";
print substr($big, 5,2); # is
print substr($big, 14); # string (till the end)
Praveen Kasireddy

Page 5
Learn PERL

print substr($big, length($big)-2, 2); # ng (from the end of the string)
print substr($big, length($big)-2); # ng (from the end of the string)
print substr($big, -2); # ng (from the end of the string)
print substr($big, 5,2,"was"); # is (original 2 characters)
print $big;

# This was a big string

uc, lc, ucfirst, lcfirst turns character(s) to upper case or lower case
Examples:
$y = "aBcD";
$x = lc($y); # $x becomes abcd
$x = uc($y); # $x becomes ABCD
$x = lcfirst($y); # $x becomes aBcD
$x = ucfirst($y); # $x becomes ABcD

6. CHOMP:
Removes exactly one newline if there is one (or more) at the end of the string
$line = <STDIN>;
chomp($line);
if ($line eq "") {
print "Blank line";
}

7. LOOPS and CONDITIONS:
You have to use {} even if there is only one statement between them !
if ($some_result) { ... }
Praveen Kasireddy

Page 6
Learn PERL

if ($age > 3) { ... }
if ($age > 3) {
...
}
else {
...
}
if ($age < 0) {
...
}
elsif ($age < 20) {
...
}
else {
...
}
UNLESS:
Sometimes you want to leave off the if part, and just do something in the else.
o

unless can do this for you:

unless ($num > 0) {
print “I said greater than 0n”;
}
WHILE:
Praveen Kasireddy

Page 7
Learn PERL

while ($num<0) {
print “Not valid, try again!n”;
print “Enter a number greater than 0n”;
$num = <STDIN>;
}
DO-WHILE:
do {
print “Enter a number greater than 0n”;
$num = <STDIN>;
} while ($num <= 0);
<STDIN> returns the value undef when it reaches End-of-File.
o undef is FALSE
while ($line = <STDIN>) {
print $line;
}
OR, AND Operators:
print “Enter a number between 1 and 100n”;
$num=<STDIN>;
if ($num<1 || $num>100) {
print “Dummyn”;
} else {
print “Great job! you entered $numn”;
}
Praveen Kasireddy

Page 8
Learn PERL

for ($j=0;$j<10;$j++) {
print “$jn”;
}
for ($x=100;$x>0;$x=$x-10) {
print “$xn”;
}
ForEach:
foreach iterates over the elements in an array (a list). A scalar variable assumes the
value of each element in the list (iteratively):

foreach $x (2,4,6) {
print “x is $xn”;
}
“$_”

$total=0;
while (<STDIN>) {
chop;
print “you entered”;
print;
$total = $total + $_;
}
print “the total is $_n”;
Praveen Kasireddy

Page 9
Learn PERL

EXERCISE:
Exercise 1
Write a program that computes the area of a rectangular ($length * $width) and prints it. Use
two variables and hard code their values.
Exercise 2
Modify the previous program to prompt for the two values (on two separate lines)
Exercise 3
Script that gets two strings (on two separate lines) and prints the concatenated version.
Exercise 4
Modify the previous area-calculator program to print a warning if one of the values is negative
and make the area 0 sized.
Exercise 5
Basic calculator that gets 2 values and an operator (+,-,*,/) (on 3 separate lines) and prints out
the result.
Exercise 6
Implement the 4 parameter version of substr using the 3 parameter version. Use the
documentation to find out what is the 4 parameter version. That is, assume your substr
function is not capable to work with 4 paramters. Write some code that would do the same
job. (hint: read the documentation to find out what does substr do with 4 parameters)
Exercise 7
You get a mathematical expression of two numbers and an operator on a single line of input
(one or more spaces separate the operator from the numbers). Calculate the result of the
expression.
(Input an be "31 * 8" or "23 / 7" etc.)
Exercise 8
Write a perl program that reads in a number and prints that number 10 times.
Exercise 9
Praveen Kasireddy

Page 10
Learn PERL

Write a perl program that reads in a number and prints an ASCII square of that size. For
example, if the number is 4, the program prints:

8. ARRAYS:
While a scalar is single value a list is a bunch of single values. A scalar value can be stored in a
scalar variable. A list can be stored in a list variable. It is called and array.
List Literals, list ranges
A list is an ordered set of scalar values.
Examples of lists:
(1, 5.2, "apple")
(1,2,3,)

# 3 values

# 3 values

(1,2,3,4,5,6,7,8,9,10)
(1..10)

# same as (1,2,3,4,5,6,7,8,9,10)

('a'..'z')

# all the lowercase letters

("joe", "peter", "mario", "cohen") # is the same as quote word
qw(joe peter mario cohen)
($a, $b, $c) = (2, 3, 7); # nearly the same as $a=2; $b=3; $c=7;
($a, $b) = (8, 1, 5); # ignore 5
($a, $b, $c) = (3, 4); # $c will be undef
An array is a variable that can hold a list, that is a list of scalars.
To get to the individual elements of the array you use the name of the array and the index of
the element.
The index can be ANY integer value, including negative numbers.
$name[0] = "Joe";
$name[1] = "Peter";
Praveen Kasireddy

Page 11
Learn PERL

$name[3] = "Cohen";
print $name[1]; # Peter
print $name[2]; # undef
Array Assignment:
You can write the following, but it is a bit too much typing
($name[0], $name[1], $name[2], $name[3]) = qw(joe peter mario cohen);
Instead you can use @ to refer to the whole array:
@name = qw(joe peter mario cohen);
You can also mix the variables on the right side and if there are arrays on the right side the
whole thing becomes one flat array !
@name = ($myname, 'joe', @oldnames);
($a, @b) = (1, 2, 3, 4); # $a is 1; @b is (2, 3, 4)
($a, @b, @x) = (1, 2, 3, 4); # $a is 1; @b is (2, 3, 4) @x is empty: ()
@a = (1, 2);
@b = (3, 4);
@c = (@a, @b); # @c becomes (1, 2, 3, 4)
In Perl there are several ways to go over all the elements of a list or an array.
Probably the most frequently used one is foreach.
foreach SCALAR (LIST) BLOCK
Example:
# this will print the names of 3 fruits:
foreach $fruit (qw(apple banana peach)) {
print "$fruitn";
Praveen Kasireddy

Page 12
Learn PERL

}
# Instead of lists you can use it with arrays:
@fruits = qw(apple banana peach);
foreach $fruit (@fruits) {
print "$fruitn";
}

9. Usage of $_
There is this strange scalar variable called $_
In Perl several functions and operators use this variable as a default variable in case no variable
is explicitely used. foreach and print are such functions. So here we can see a shorter version of
the previous script:
@fruits = qw(apple banana peach);
foreach $fruit (@fruits) {
print $fruit;
}
foreach $_ (@fruits) {
print $_;
}
# but the real use of the variable is when it is not used explicitly:
foreach (@fruits) {
print ;
}
Print Arrays:
Praveen Kasireddy

Page 13
Learn PERL

@a = qw(x y z);
print "pre ", @a, " postn";
# pre xyz post
# $, holds empty string
print "pre @a postn";
POP& PUSH, SHIFT and UNSHIFT
There are several functions working on arrays:
pop and push implement a LIFO stack.
pop fetches the last element of the array returns that value and the array becomes one shorter
if the array was empty pop returns undef
SCALAR = pop ARRAY;
Example:
@array = ('a', 'b', 'c');
$lastelem = pop(@array); # $lastelem gets 'c'

@array becomes ('a', 'b')

push is the opposite of pop it adds element(s) to the end of the array It returns number of
elements after the push.
PUSH ARRAY, SCALAR, ... (more SCALARs);
Example:
@array = ('a', 'b');
push(@array, 'd'); # @array becomes ('a', 'b', 'd')
push(@array, 'e', 'f'); # @array becomes ('a', 'b', 'd', 'e', 'f')
shift and unshift are working on the beginning (left side) of the array.
shift fetches the first element of an array.

Praveen Kasireddy

Page 14
Learn PERL

It returns the fetched element and the whole array becomes one shorter and moved to the left.
Returns undef if the array was empty.
Example:
@array = ('a', 'b', 'c');
$f = shift @array; # $f gets 'a', @array becomes ('b', 'c')
unshift adds element(s) to the beginning of an array returns number of elements in the array
after the addition
Example:
@array = ('b', 'c');
unshift(@array, 'x', 'y') # @array becomes ('x', 'y', 'b', 'c')
Exercise 1
Implement pop,push,shift,unshift (only with @ $ and array index)
Example: pop is used like this:
$x = pop @a;
implementation:
$x = $a[$#a];
$#a = $#a - 1;
Exercise 2
Create a menu system where the user can select a fruit by giving the corresponding number (17) and you print the name of the fruit. (The list of the fruits is hard coded)
Exercise 3
Read in a few numbers, each one on a separate line till end-of-input and print out the min, max
and average. (End-of-input is ^D on linux and ^Z ENTER on windows.)
23
11
Praveen Kasireddy

Page 15
Learn PERL

198
3
-70
1001
Exercise 4
Read in a few lines (till end-of-input) and print them in reverse order.
Exercise 5
Write a program that generates the first n Fibonacci numbers. (Definition: f(n) = f(n-1)+f(n-2),
f(0)=f(1)=1) The user supplies the value of n.
Example:
INPUT : 6
OUTPUT: 1 1 2 3 5 8

10.

Sub Routines:

sub foo {
"Hi Dave";
}

Calling Sub routines:
In the old days, the name of a subroutine started with '&' (doesn't have to any more).
These are all valid calls of the subroutine:
&foo; foo(1,2,3,4);
&foo("Hello"); foo 11.75;
** You can use the my keyword to declare a variable as private to the subroutine:
my($a,$b);
Praveen Kasireddy

Page 16
Learn PERL

my($size) = 11;
my(@foo) = (1,3..11);
Sample Sub routine:
sub min {
my($a,$b) = @_;
if ($a < $b) {
$a;
} else {
$b;
}
}

11.

PERL DB

Sample 1:
use Win32::ODBC;
# Create a database object and make sure
# the database was found
$db = new Win32::ODBC("eiw");
if (! $db) {
print "Error - the eiw database could not be foundn");
...
}

Praveen Kasireddy

Page 17
Learn PERL

Sample 2
use DBI;
$db=DBI->connect("dbi:ODBC:Flight32");
$a=$db->prepare("Select * from orders");
$a->execute;
@row=$a->fetchrow_array;
print @row;

12.

PERL FILE HANDLING

PERL reads files using various syntaxes :
open a file for reading, and link it to a filehandle:
open IN, “C:EHD.txt";
And then read lines from the filehandle, exactly like you would from <STDIN>:
$line = <IN>;
foreach $line (<IN>) ...
And don’t forget to close it:
close IN;
A nice way to check that the open didn’t fail (e.g. if the file doesn’t exists):
open IN, "$file" or die "can't open file $file";
PERL can also write into files:
open a file for writing, and link it to a filehandle:
open OUT, ">EHD.analysis";
(If a file by that name already exists it will be overwriten!)
Or, you can add lines at the end of an existing file (append):
open OUT, ">>EHD.analysis";
Print to a file:
print OUT "The mutation is in exon $exonNumbern";
Praveen Kasireddy

Page 18
Learn PERL

PERL understands various representations of folders paths :
open IN, '<D:EyalPERLp53.fasta';
n
n

Always use a full path name, it is safer and clearer to read
Remember to use  in double qoutes

open IN, "<D:EyalPERL$name.fasta";
n

You can also use / (usually…)

open IN, "<D:/Eyal/PERL/$name.fasta";
PERL also can work with foldersand files inside them:
Perl allows easy access to the files in a directory by “globbing”:
foreach $fileName (<D:/scripts/*.pl>) {
open IN, $fileName
or die "can't open file $fileName";
foreach $line (<IN>) {
do something...
}
}

More samples:
open OF, "<C:File1.txt";
foreach $l(<OF>)
{
print "$ln";
}
open WF, ">>C:File1.txt";
print WF "nThis is additional Line";

Praveen Kasireddy

Page 19
Learn PERL

13.

PERL REGULAR EXPRESSION

Perl’s greatest capability is its regular expressions. It uses “/<expression> /” for applying regular
expression.
Sample:
print (" please enter your emailIDn");
$_=<STDIN>;
if (/[0-9].*@.*/)
{
print "It is a correct mail format";
}
else
{
print "it is a wrong mail format";
}
#Q> Write a text file with 10 mail ids (Valid and Invalid).
#Write a perl scrip which reads the file and copy all valid #emails to a new file.

14.

PERL HASH

What is a hash and why to use one?
 Unordered group of key/value pairs where
 key is a unique string and
 value is any scalar
Also called Associative Array Like an array but the 'index' is string instead of number.
Examples of use, some kind of mapping:
 phone book (name => phone number)
 worker list (ID number => name)
 DNS resolution (hostname => IP address)
 UNIX admin (username => UID)
Praveen Kasireddy

Page 20
Learn PERL

 Windows (Netbios name of computer => Name of person using it)
 CGI: (fieldname => field value)
Sample1:
Accessing Individual Hash Elements
$phone{"Yossi Cohen"} = "03-672234";
# We create a hash called %phone and give value
to
print $phone{"Yossi Cohen"};
# one of its elements. then we can use that
element
$count_votes{"Bush"}++;
# in another hash we use the value as a number
$name = "Bush";
# We can use variables in place of the key
$count_votes{$name}--;
# The variable springs to existence.
# The original value is undef.
# before accessing a pair you should check if
if (exists $phone{"George"}) {
# the key exists at all
if (defined $phone{"George"}) {
# the value has been defined
print $phone{"George"};
}
}
# if you had enough from George
delete $phone{"George"};
# delete him from your phone book:

Sample 2
The whole hash
# The whole hash is called %phone
%phone = ("Yossi", 123, "Joe", 456, "Mary", 678);
%phone = (
# Maybe that is more readable like this
"Yossi", 123,
"Joe", 456,
"Mary", 678);
%phone = (
# or even better with the big arrow
"Yossi" => 123,
"Joe" => 456,
"Mary" => 678);
# If we get an array like this from somewhere, we can turn it to a hash but beware of the
change of meaning !
@array = ("Yossi", 123, "Joe", 456, "Mary", 678);
%phone = @array;

Praveen Kasireddy

Page 21

Weitere ähnliche Inhalte

Was ist angesagt? (19)

Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 
Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Oops in php
Oops in phpOops in php
Oops in php
 
Perl
PerlPerl
Perl
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
Wx::Perl::Smart
Wx::Perl::SmartWx::Perl::Smart
Wx::Perl::Smart
 
Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6
 
Php array
Php arrayPhp array
Php array
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
 
Functional programming with php7
Functional programming with php7Functional programming with php7
Functional programming with php7
 
Descobrindo a linguagem Perl
Descobrindo a linguagem PerlDescobrindo a linguagem Perl
Descobrindo a linguagem Perl
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 

Andere mochten auch

Sql commands worked out in sql plus with screen shots
Sql commands worked out in sql plus with screen shotsSql commands worked out in sql plus with screen shots
Sql commands worked out in sql plus with screen shotsSunil Kumar Gunasekaran
 
Accenture Case Study Solution by Amit Bhardwaj
Accenture Case Study Solution by Amit BhardwajAccenture Case Study Solution by Amit Bhardwaj
Accenture Case Study Solution by Amit BhardwajAmit Bhardwaj
 
Sample Technical Requirement Document (TRD)
Sample Technical Requirement Document (TRD)Sample Technical Requirement Document (TRD)
Sample Technical Requirement Document (TRD)Sunil Kumar Gunasekaran
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Sunil Kumar Gunasekaran
 
Fitnesse user acceptance test - Presentation
Fitnesse   user acceptance test - PresentationFitnesse   user acceptance test - Presentation
Fitnesse user acceptance test - PresentationSunil Kumar Gunasekaran
 
How to pass a coding interview as an automation developer talk - Oct 17 2016
How to pass a coding interview as an automation developer talk - Oct 17 2016How to pass a coding interview as an automation developer talk - Oct 17 2016
How to pass a coding interview as an automation developer talk - Oct 17 2016Thomas F. "T.J." Maher Jr.
 

Andere mochten auch (13)

CQL - Cassandra commands Notes
CQL - Cassandra commands NotesCQL - Cassandra commands Notes
CQL - Cassandra commands Notes
 
Sql commands worked out in sql plus with screen shots
Sql commands worked out in sql plus with screen shotsSql commands worked out in sql plus with screen shots
Sql commands worked out in sql plus with screen shots
 
Test process - Important Concepts
Test process - Important ConceptsTest process - Important Concepts
Test process - Important Concepts
 
Accenture Case Study Solution by Amit Bhardwaj
Accenture Case Study Solution by Amit BhardwajAccenture Case Study Solution by Amit Bhardwaj
Accenture Case Study Solution by Amit Bhardwaj
 
Sample Technical Requirement Document (TRD)
Sample Technical Requirement Document (TRD)Sample Technical Requirement Document (TRD)
Sample Technical Requirement Document (TRD)
 
Scrum, V Model and RUP Models Overview
Scrum, V Model and RUP Models OverviewScrum, V Model and RUP Models Overview
Scrum, V Model and RUP Models Overview
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
 
Fitnesse user acceptance test - Presentation
Fitnesse   user acceptance test - PresentationFitnesse   user acceptance test - Presentation
Fitnesse user acceptance test - Presentation
 
Java J2EE Complete Syllabus Checklist
Java J2EE Complete Syllabus ChecklistJava J2EE Complete Syllabus Checklist
Java J2EE Complete Syllabus Checklist
 
Wells fargo banking system ER Diagram
Wells fargo banking system ER DiagramWells fargo banking system ER Diagram
Wells fargo banking system ER Diagram
 
Ecommerce Website Testing Checklist
Ecommerce Website Testing ChecklistEcommerce Website Testing Checklist
Ecommerce Website Testing Checklist
 
How to pass a coding interview as an automation developer talk - Oct 17 2016
How to pass a coding interview as an automation developer talk - Oct 17 2016How to pass a coding interview as an automation developer talk - Oct 17 2016
How to pass a coding interview as an automation developer talk - Oct 17 2016
 
Amazon search test case document
Amazon search test case documentAmazon search test case document
Amazon search test case document
 

Ähnlich wie PERL for QA - Important Commands and applications

Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationMohammed Farrag
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tourSimon Proctor
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tourSimon Proctor
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?osfameron
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1Dave Cross
 
Scripting3
Scripting3Scripting3
Scripting3Nao Dara
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrowPete McFarlane
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlDave Cross
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with GroovyArturo Herrero
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfezhilvizhiyan
 

Ähnlich wie PERL for QA - Important Commands and applications (20)

Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
 
Perl slid
Perl slidPerl slid
Perl slid
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Scripting3
Scripting3Scripting3
Scripting3
 
How to write code you won't hate tomorrow
How to write code you won't hate tomorrowHow to write code you won't hate tomorrow
How to write code you won't hate tomorrow
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
PERL.ppt
PERL.pptPERL.ppt
PERL.ppt
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
 

Mehr von Sunil Kumar Gunasekaran

Business Requirements Document for Acounts Payable System
Business Requirements Document for Acounts Payable SystemBusiness Requirements Document for Acounts Payable System
Business Requirements Document for Acounts Payable SystemSunil Kumar Gunasekaran
 
Test Life Cycle - Presentation - Important concepts covered
Test Life Cycle - Presentation - Important concepts coveredTest Life Cycle - Presentation - Important concepts covered
Test Life Cycle - Presentation - Important concepts coveredSunil Kumar Gunasekaran
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Hw1 project charter electronic health record for university health care_sunil...
Hw1 project charter electronic health record for university health care_sunil...Hw1 project charter electronic health record for university health care_sunil...
Hw1 project charter electronic health record for university health care_sunil...Sunil Kumar Gunasekaran
 
Hw2 gap analysis linked_in mobile app_sunil kumar gunasekaran_12052012.docx
Hw2 gap analysis linked_in mobile app_sunil kumar gunasekaran_12052012.docxHw2 gap analysis linked_in mobile app_sunil kumar gunasekaran_12052012.docx
Hw2 gap analysis linked_in mobile app_sunil kumar gunasekaran_12052012.docxSunil Kumar Gunasekaran
 

Mehr von Sunil Kumar Gunasekaran (15)

Actual test case document
Actual test case documentActual test case document
Actual test case document
 
Sql reference from w3 schools
Sql reference from w3 schools Sql reference from w3 schools
Sql reference from w3 schools
 
Business Requirements Document for Acounts Payable System
Business Requirements Document for Acounts Payable SystemBusiness Requirements Document for Acounts Payable System
Business Requirements Document for Acounts Payable System
 
Automation Testing Syllabus - Checklist
Automation Testing Syllabus - ChecklistAutomation Testing Syllabus - Checklist
Automation Testing Syllabus - Checklist
 
Unix short
Unix shortUnix short
Unix short
 
Unix made easy
Unix made easyUnix made easy
Unix made easy
 
Testing http methods using Telnet
Testing http methods using TelnetTesting http methods using Telnet
Testing http methods using Telnet
 
Test Life Cycle - Presentation - Important concepts covered
Test Life Cycle - Presentation - Important concepts coveredTest Life Cycle - Presentation - Important concepts covered
Test Life Cycle - Presentation - Important concepts covered
 
Scrum writeup - Agile
Scrum writeup - Agile Scrum writeup - Agile
Scrum writeup - Agile
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Web services SOAP Notes
Web services SOAP NotesWeb services SOAP Notes
Web services SOAP Notes
 
Biz req doc for acounts payable system
Biz req  doc for acounts payable systemBiz req  doc for acounts payable system
Biz req doc for acounts payable system
 
Hw1 project charter electronic health record for university health care_sunil...
Hw1 project charter electronic health record for university health care_sunil...Hw1 project charter electronic health record for university health care_sunil...
Hw1 project charter electronic health record for university health care_sunil...
 
Hw2 gap analysis linked_in mobile app_sunil kumar gunasekaran_12052012.docx
Hw2 gap analysis linked_in mobile app_sunil kumar gunasekaran_12052012.docxHw2 gap analysis linked_in mobile app_sunil kumar gunasekaran_12052012.docx
Hw2 gap analysis linked_in mobile app_sunil kumar gunasekaran_12052012.docx
 

Kürzlich hochgeladen

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

PERL for QA - Important Commands and applications

  • 1. Learn PERL PERL - Practical Extraction and Reporting Language 1. Where do we use PERL: ........................................................................................................................ 2 2. What are the key features of PERL: ...................................................................................................... 2 3. SCALAR Data: ........................................................................................................................................ 2 4. USER IO: ................................................................................................................................................ 5 5. STRING OPERATIONS: ........................................................................................................................... 5 6. CHOMP: ................................................................................................................................................. 6 7. LOOPS and CONDITIONS: ...................................................................................................................... 6 8. ARRAYS: ............................................................................................................................................... 11 9. Usage of $_ ......................................................................................................................................... 13 10. Sub Routines: .................................................................................................................................. 16 11. PERL DB ........................................................................................................................................... 17 12. PERL FILE HANDLING ....................................................................................................................... 18 13. PERL REGULAR EXPRESSION ........................................................................................................... 20 14. PERL HASH....................................................................................................................................... 20 Praveen Kasireddy Page 1
  • 2. Learn PERL 1. Where do we use PERL: Quick and dirty programs (automating simple daily jobs) System Administration Web backend (CGI/Database access) Configuration Management QA Biotechnology Prototypes 2. What are the key features of PERL: Text processing (pattern matching) List processing Database access System language To know if PERL is installed in your machine, you can try perl –v or perl –V Sample PERL code: perl -e "print 1000" will print à1000 # is a comment. perldoc provides documentation about any one command 3. SCALAR Data: A single piece of data either a number or a string is called a 'scalar' in Perl. PERL Takes care of data types on its own: print 3+4; #7 print 9-7; #2 print 2*3; #6 print 3/0; # ERROR - Even Perl cannot divide by 0 Print 11 % 3; # 2 (modulus) Praveen Kasireddy Page 2
  • 3. Learn PERL print 2 ** 3; # 8 (power of) print "another 'string'"; # another 'string' print "escape" this"; # ERROR print "escape" this"; # escape" this STRING Operators: print "a" . "b"; # ab - concatenation print "e" x 3; # eee - repetition print "perl " x 3; # perl perl perl print "-" x 80; # ------- (80 times) SCALAR Variable declaration: To save the values for later use in your program you need variables. Rules:    Start with letter or underscore Contains letters, underscores and digits Case sensitive ** Same scalar variable can store integers and strings: $x = 3; $y = $x + 2; $z = "hello world"; $z = 8; String TO Number Conversion: $x = "3 apples"; $y = "5 banana"; Praveen Kasireddy Page 3
  • 4. Learn PERL $total = $x + $y; print $total; # 8 # Using a numerical operator forces the strings to be numbers print "3a2" * 4; # 12 print "7683a2" * 1; # 7683 print "a2" * 4; # 0 print "010a2" + 4; # 14 print "3.1" + 0; # 3.1 print "3e+2x" + 0; # 300 Special Observation: print "2" + 3; # 5 - (+) is a numerical operator print "2" . 3; # 23 - (.) is a string operator Variable Interpolation: $c = 3; $what = "apples"; $q = "$c $what"; print $q; # 3 apples works in double quoted strings only print '$c $what'; # $c $what Variable name $what = "banana"; $c = 3; print "$c $what"; # 3 banana Praveen Kasireddy Page 4
  • 5. Learn PERL print "$c $whats"; # 3 (warning) print "$c ${what}sn"; # this works well $c = 3; $what = 'apple'; print "$c $what sn"; print $c . " " . $what . " sn"; print $c, " ", $what , " sn"; # all will result in '3 apple s' followed by a newline 4. USER IO: $line = <STDIN>; # To Accept User inputs if ($line eq "") { print "Blank line"; } print $line; 5. STRING OPERATIONS: substr returns a substring of a big string $part = substr($big, $index_of_first, $length); Examples: $big = "This is a big string"; print substr($big, 5,2); # is print substr($big, 14); # string (till the end) Praveen Kasireddy Page 5
  • 6. Learn PERL print substr($big, length($big)-2, 2); # ng (from the end of the string) print substr($big, length($big)-2); # ng (from the end of the string) print substr($big, -2); # ng (from the end of the string) print substr($big, 5,2,"was"); # is (original 2 characters) print $big; # This was a big string uc, lc, ucfirst, lcfirst turns character(s) to upper case or lower case Examples: $y = "aBcD"; $x = lc($y); # $x becomes abcd $x = uc($y); # $x becomes ABCD $x = lcfirst($y); # $x becomes aBcD $x = ucfirst($y); # $x becomes ABcD 6. CHOMP: Removes exactly one newline if there is one (or more) at the end of the string $line = <STDIN>; chomp($line); if ($line eq "") { print "Blank line"; } 7. LOOPS and CONDITIONS: You have to use {} even if there is only one statement between them ! if ($some_result) { ... } Praveen Kasireddy Page 6
  • 7. Learn PERL if ($age > 3) { ... } if ($age > 3) { ... } else { ... } if ($age < 0) { ... } elsif ($age < 20) { ... } else { ... } UNLESS: Sometimes you want to leave off the if part, and just do something in the else. o unless can do this for you: unless ($num > 0) { print “I said greater than 0n”; } WHILE: Praveen Kasireddy Page 7
  • 8. Learn PERL while ($num<0) { print “Not valid, try again!n”; print “Enter a number greater than 0n”; $num = <STDIN>; } DO-WHILE: do { print “Enter a number greater than 0n”; $num = <STDIN>; } while ($num <= 0); <STDIN> returns the value undef when it reaches End-of-File. o undef is FALSE while ($line = <STDIN>) { print $line; } OR, AND Operators: print “Enter a number between 1 and 100n”; $num=<STDIN>; if ($num<1 || $num>100) { print “Dummyn”; } else { print “Great job! you entered $numn”; } Praveen Kasireddy Page 8
  • 9. Learn PERL for ($j=0;$j<10;$j++) { print “$jn”; } for ($x=100;$x>0;$x=$x-10) { print “$xn”; } ForEach: foreach iterates over the elements in an array (a list). A scalar variable assumes the value of each element in the list (iteratively): foreach $x (2,4,6) { print “x is $xn”; } “$_” $total=0; while (<STDIN>) { chop; print “you entered”; print; $total = $total + $_; } print “the total is $_n”; Praveen Kasireddy Page 9
  • 10. Learn PERL EXERCISE: Exercise 1 Write a program that computes the area of a rectangular ($length * $width) and prints it. Use two variables and hard code their values. Exercise 2 Modify the previous program to prompt for the two values (on two separate lines) Exercise 3 Script that gets two strings (on two separate lines) and prints the concatenated version. Exercise 4 Modify the previous area-calculator program to print a warning if one of the values is negative and make the area 0 sized. Exercise 5 Basic calculator that gets 2 values and an operator (+,-,*,/) (on 3 separate lines) and prints out the result. Exercise 6 Implement the 4 parameter version of substr using the 3 parameter version. Use the documentation to find out what is the 4 parameter version. That is, assume your substr function is not capable to work with 4 paramters. Write some code that would do the same job. (hint: read the documentation to find out what does substr do with 4 parameters) Exercise 7 You get a mathematical expression of two numbers and an operator on a single line of input (one or more spaces separate the operator from the numbers). Calculate the result of the expression. (Input an be "31 * 8" or "23 / 7" etc.) Exercise 8 Write a perl program that reads in a number and prints that number 10 times. Exercise 9 Praveen Kasireddy Page 10
  • 11. Learn PERL Write a perl program that reads in a number and prints an ASCII square of that size. For example, if the number is 4, the program prints: 8. ARRAYS: While a scalar is single value a list is a bunch of single values. A scalar value can be stored in a scalar variable. A list can be stored in a list variable. It is called and array. List Literals, list ranges A list is an ordered set of scalar values. Examples of lists: (1, 5.2, "apple") (1,2,3,) # 3 values # 3 values (1,2,3,4,5,6,7,8,9,10) (1..10) # same as (1,2,3,4,5,6,7,8,9,10) ('a'..'z') # all the lowercase letters ("joe", "peter", "mario", "cohen") # is the same as quote word qw(joe peter mario cohen) ($a, $b, $c) = (2, 3, 7); # nearly the same as $a=2; $b=3; $c=7; ($a, $b) = (8, 1, 5); # ignore 5 ($a, $b, $c) = (3, 4); # $c will be undef An array is a variable that can hold a list, that is a list of scalars. To get to the individual elements of the array you use the name of the array and the index of the element. The index can be ANY integer value, including negative numbers. $name[0] = "Joe"; $name[1] = "Peter"; Praveen Kasireddy Page 11
  • 12. Learn PERL $name[3] = "Cohen"; print $name[1]; # Peter print $name[2]; # undef Array Assignment: You can write the following, but it is a bit too much typing ($name[0], $name[1], $name[2], $name[3]) = qw(joe peter mario cohen); Instead you can use @ to refer to the whole array: @name = qw(joe peter mario cohen); You can also mix the variables on the right side and if there are arrays on the right side the whole thing becomes one flat array ! @name = ($myname, 'joe', @oldnames); ($a, @b) = (1, 2, 3, 4); # $a is 1; @b is (2, 3, 4) ($a, @b, @x) = (1, 2, 3, 4); # $a is 1; @b is (2, 3, 4) @x is empty: () @a = (1, 2); @b = (3, 4); @c = (@a, @b); # @c becomes (1, 2, 3, 4) In Perl there are several ways to go over all the elements of a list or an array. Probably the most frequently used one is foreach. foreach SCALAR (LIST) BLOCK Example: # this will print the names of 3 fruits: foreach $fruit (qw(apple banana peach)) { print "$fruitn"; Praveen Kasireddy Page 12
  • 13. Learn PERL } # Instead of lists you can use it with arrays: @fruits = qw(apple banana peach); foreach $fruit (@fruits) { print "$fruitn"; } 9. Usage of $_ There is this strange scalar variable called $_ In Perl several functions and operators use this variable as a default variable in case no variable is explicitely used. foreach and print are such functions. So here we can see a shorter version of the previous script: @fruits = qw(apple banana peach); foreach $fruit (@fruits) { print $fruit; } foreach $_ (@fruits) { print $_; } # but the real use of the variable is when it is not used explicitly: foreach (@fruits) { print ; } Print Arrays: Praveen Kasireddy Page 13
  • 14. Learn PERL @a = qw(x y z); print "pre ", @a, " postn"; # pre xyz post # $, holds empty string print "pre @a postn"; POP& PUSH, SHIFT and UNSHIFT There are several functions working on arrays: pop and push implement a LIFO stack. pop fetches the last element of the array returns that value and the array becomes one shorter if the array was empty pop returns undef SCALAR = pop ARRAY; Example: @array = ('a', 'b', 'c'); $lastelem = pop(@array); # $lastelem gets 'c' @array becomes ('a', 'b') push is the opposite of pop it adds element(s) to the end of the array It returns number of elements after the push. PUSH ARRAY, SCALAR, ... (more SCALARs); Example: @array = ('a', 'b'); push(@array, 'd'); # @array becomes ('a', 'b', 'd') push(@array, 'e', 'f'); # @array becomes ('a', 'b', 'd', 'e', 'f') shift and unshift are working on the beginning (left side) of the array. shift fetches the first element of an array. Praveen Kasireddy Page 14
  • 15. Learn PERL It returns the fetched element and the whole array becomes one shorter and moved to the left. Returns undef if the array was empty. Example: @array = ('a', 'b', 'c'); $f = shift @array; # $f gets 'a', @array becomes ('b', 'c') unshift adds element(s) to the beginning of an array returns number of elements in the array after the addition Example: @array = ('b', 'c'); unshift(@array, 'x', 'y') # @array becomes ('x', 'y', 'b', 'c') Exercise 1 Implement pop,push,shift,unshift (only with @ $ and array index) Example: pop is used like this: $x = pop @a; implementation: $x = $a[$#a]; $#a = $#a - 1; Exercise 2 Create a menu system where the user can select a fruit by giving the corresponding number (17) and you print the name of the fruit. (The list of the fruits is hard coded) Exercise 3 Read in a few numbers, each one on a separate line till end-of-input and print out the min, max and average. (End-of-input is ^D on linux and ^Z ENTER on windows.) 23 11 Praveen Kasireddy Page 15
  • 16. Learn PERL 198 3 -70 1001 Exercise 4 Read in a few lines (till end-of-input) and print them in reverse order. Exercise 5 Write a program that generates the first n Fibonacci numbers. (Definition: f(n) = f(n-1)+f(n-2), f(0)=f(1)=1) The user supplies the value of n. Example: INPUT : 6 OUTPUT: 1 1 2 3 5 8 10. Sub Routines: sub foo { "Hi Dave"; } Calling Sub routines: In the old days, the name of a subroutine started with '&' (doesn't have to any more). These are all valid calls of the subroutine: &foo; foo(1,2,3,4); &foo("Hello"); foo 11.75; ** You can use the my keyword to declare a variable as private to the subroutine: my($a,$b); Praveen Kasireddy Page 16
  • 17. Learn PERL my($size) = 11; my(@foo) = (1,3..11); Sample Sub routine: sub min { my($a,$b) = @_; if ($a < $b) { $a; } else { $b; } } 11. PERL DB Sample 1: use Win32::ODBC; # Create a database object and make sure # the database was found $db = new Win32::ODBC("eiw"); if (! $db) { print "Error - the eiw database could not be foundn"); ... } Praveen Kasireddy Page 17
  • 18. Learn PERL Sample 2 use DBI; $db=DBI->connect("dbi:ODBC:Flight32"); $a=$db->prepare("Select * from orders"); $a->execute; @row=$a->fetchrow_array; print @row; 12. PERL FILE HANDLING PERL reads files using various syntaxes : open a file for reading, and link it to a filehandle: open IN, “C:EHD.txt"; And then read lines from the filehandle, exactly like you would from <STDIN>: $line = <IN>; foreach $line (<IN>) ... And don’t forget to close it: close IN; A nice way to check that the open didn’t fail (e.g. if the file doesn’t exists): open IN, "$file" or die "can't open file $file"; PERL can also write into files: open a file for writing, and link it to a filehandle: open OUT, ">EHD.analysis"; (If a file by that name already exists it will be overwriten!) Or, you can add lines at the end of an existing file (append): open OUT, ">>EHD.analysis"; Print to a file: print OUT "The mutation is in exon $exonNumbern"; Praveen Kasireddy Page 18
  • 19. Learn PERL PERL understands various representations of folders paths : open IN, '<D:EyalPERLp53.fasta'; n n Always use a full path name, it is safer and clearer to read Remember to use in double qoutes open IN, "<D:EyalPERL$name.fasta"; n You can also use / (usually…) open IN, "<D:/Eyal/PERL/$name.fasta"; PERL also can work with foldersand files inside them: Perl allows easy access to the files in a directory by “globbing”: foreach $fileName (<D:/scripts/*.pl>) { open IN, $fileName or die "can't open file $fileName"; foreach $line (<IN>) { do something... } } More samples: open OF, "<C:File1.txt"; foreach $l(<OF>) { print "$ln"; } open WF, ">>C:File1.txt"; print WF "nThis is additional Line"; Praveen Kasireddy Page 19
  • 20. Learn PERL 13. PERL REGULAR EXPRESSION Perl’s greatest capability is its regular expressions. It uses “/<expression> /” for applying regular expression. Sample: print (" please enter your emailIDn"); $_=<STDIN>; if (/[0-9].*@.*/) { print "It is a correct mail format"; } else { print "it is a wrong mail format"; } #Q> Write a text file with 10 mail ids (Valid and Invalid). #Write a perl scrip which reads the file and copy all valid #emails to a new file. 14. PERL HASH What is a hash and why to use one?  Unordered group of key/value pairs where  key is a unique string and  value is any scalar Also called Associative Array Like an array but the 'index' is string instead of number. Examples of use, some kind of mapping:  phone book (name => phone number)  worker list (ID number => name)  DNS resolution (hostname => IP address)  UNIX admin (username => UID) Praveen Kasireddy Page 20
  • 21. Learn PERL  Windows (Netbios name of computer => Name of person using it)  CGI: (fieldname => field value) Sample1: Accessing Individual Hash Elements $phone{"Yossi Cohen"} = "03-672234"; # We create a hash called %phone and give value to print $phone{"Yossi Cohen"}; # one of its elements. then we can use that element $count_votes{"Bush"}++; # in another hash we use the value as a number $name = "Bush"; # We can use variables in place of the key $count_votes{$name}--; # The variable springs to existence. # The original value is undef. # before accessing a pair you should check if if (exists $phone{"George"}) { # the key exists at all if (defined $phone{"George"}) { # the value has been defined print $phone{"George"}; } } # if you had enough from George delete $phone{"George"}; # delete him from your phone book: Sample 2 The whole hash # The whole hash is called %phone %phone = ("Yossi", 123, "Joe", 456, "Mary", 678); %phone = ( # Maybe that is more readable like this "Yossi", 123, "Joe", 456, "Mary", 678); %phone = ( # or even better with the big arrow "Yossi" => 123, "Joe" => 456, "Mary" => 678); # If we get an array like this from somewhere, we can turn it to a hash but beware of the change of meaning ! @array = ("Yossi", 123, "Joe", 456, "Mary", 678); %phone = @array; Praveen Kasireddy Page 21