24
Learn PERL PERL - P ractical E xtraction and R eporting L anguage 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

PERL for QA - Important Commands and applications

Embed Size (px)

Citation preview

Page 1: PERL for QA - Important Commands and applications

Learn PERL

PERL - P ractical E xtraction and R eporting L anguage

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

Page 2: PERL for QA - Important Commands and applications

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

Page 3: PERL for QA - Important Commands and applications

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

Page 4: PERL for QA - Important Commands and applications

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

Page 5: PERL for QA - Important Commands and applications

Learn PERL

print "$c $whats"; # 3 (warning)

print "$c ${what}s\n"; # this works well

$c = 3;

$what = 'apple';

print "$c $what s\n";

print $c . " " . $what . " s\n";

print $c, " ", $what , " s\n";

# 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

Page 6: PERL for QA - Important Commands and applications

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

Page 7: PERL for QA - Important Commands and applications

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 0\n”;

}

WHILE:

Praveen Kasireddy Page 7

Page 8: PERL for QA - Important Commands and applications

Learn PERL

while ($num<0) {

print “Not valid, try again!\n”;

print “Enter a number greater than 0\n”;

$num = <STDIN>;

}

DO-WHILE:

do {

print “Enter a number greater than 0\n”;

$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 100\n”;

$num=<STDIN>;

if ($num<1 || $num>100) {

print “Dummy\n”;

} else {

print “Great job! you entered $num\n”;

}

Praveen Kasireddy Page 8

Page 9: PERL for QA - Important Commands and applications

Learn PERL

for ($j=0;$j<10;$j++) {

print “$j\n”;

}

for ($x=100;$x>0;$x=$x-10) {

print “$x\n”;

}

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 $x\n”;

}

“$_”

$total=0;

while (<STDIN>) {

chop;

print “you entered”;

print;

$total = $total + $_;

}

print “the total is $_\n”;

Praveen Kasireddy Page 9

Page 10: PERL for QA - Important Commands and applications

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

Page 11: PERL for QA - Important Commands and applications

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") # 3 values

(1,2,3,) # 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

Page 12: PERL for QA - Important Commands and applications

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 "$fruit\n";

Praveen Kasireddy Page 12

Page 13: PERL for QA - Important Commands and applications

Learn PERL

}

# Instead of lists you can use it with arrays:

@fruits = qw(apple banana peach);

foreach $fruit (@fruits) {

print "$fruit\n";

}

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

Page 14: PERL for QA - Important Commands and applications

Learn PERL

@a = qw(x y z);

print "pre ", @a, " post\n";

# pre xyz post

# $, holds empty string

print "pre @a post\n";

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

Page 15: PERL for QA - Important Commands and applications

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 (1-7) 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

Page 16: PERL for QA - Important Commands and applications

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

Page 17: PERL for QA - Important Commands and applications

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 found\n");

...

}

Praveen Kasireddy Page 17

Page 18: PERL for QA - Important Commands and applications

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 $exonNumber\n";

Praveen Kasireddy Page 18

Page 19: PERL for QA - Important Commands and applications

Learn PERL

PERL understands various representations of folders paths :

open IN, '<D:\Eyal\PERL\p53.fasta';

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

open IN, "<D:\\Eyal\\PERL\\$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 "$l\n";

}

open WF, ">>C:\\File1.txt";

print WF "\nThis is additional Line";

Praveen Kasireddy Page 19

Page 20: PERL for QA - Important Commands and applications

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 emailID\n");

$_=<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 HASHWhat 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

Page 21: PERL for QA - Important Commands and applications

Learn PERL

Windows (Netbios name of computer => Name of person using it) CGI: (fieldname => field value)

Sample1:Accessing Individual Hash ElementsAccessing 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 2Sample 2The whole hashThe 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