You are on page 1of 25

Introduction to Perl

programming

Perl history
Practical Extraction and Report Language
developed by Larry Wall in year1987
Developed as a general-purpose Unix
scripting language to make report processing
easier.
It has undergone many changes and
revisions and become widely popular
amongst programmers.
Latest Perl version is _____

Larr y

Operators

Operators are special symbols or words

that describe an operation or an action to


take place between two or more values.

Operators are used in formulas.

The program reads the operators in a

formula and performs the actions specified.

Arithmetic operators

Simple operators of interest: + , - , * , /


Respectively adds, subtracts, multiplies and
divides two floating point numbers.
a ** b
Raises "a" to the power of "b". Works on
floating point numbers too.
a.b
Concatenates two strings.

a%b
Returns the remainder of "b" from "a".
4

Variables

Variables hold variable pieces of information they are just storage containers for numbers,
strings, and compound structures

Variables do not need to be declared

Data type (int, char, ...) is decided at run time

Three major types of variables in Perl are,


o
o
o

Scalar
Array
Hash
5

Scalar
A scalar holds numbers and strings,
Scalar variable name starts with $ and

Example
$a = 5;

$language = perl;

length function - function returns the length,


in characters, of the supplied string.
Example
print length($language); #output 4

Simple example

Write a program to print Hello World.


print Hello World;
OR
$string= Hello World;
print $string;
OR
Take input from user
7

Case modification in string


The four basic functions are,
lc
uc
lcfirst
ucfirst
Example

$string = "The Cat Sat on the Mat";

Array

An ordered collection of scalars

Array variable is denoted by the @


Example

@char = ( Rancho, Farhan, Raju );

To access whole array,


print @char; # prints : Rancho Farhan Raju
*Notice that you do not need to loop
through the whole array to print it.
9

Array Index

Array Indexes start at 0 !!!

To access one element of the array : use $


Why? Because every element in the array

is scalar

Example

@char = ( Rancho, Farhan, Raju );


print $char[0]\n; # prints : Rancho
10

Array index

To find the index of the last element in the


array,
@array = ( Rancho, Farhan, Raju );
print $#array; #output 2

Array size

To find the number of elements in the array:


$array_size = @array;
$array_size now has 3 in the above
example
because there are 3 elements in the array
11

split and join functions

split function- Explodes strings into arrays


$my_dna= "atgggaccatgcgatttagctcg";
@dna= split('',$my_dna);
print "@dna";

join function- Explodes arrays into scalars


@char= (Rancho, Farhan, Raju);
$names= join('',@char);
print $names;
12

Built-in functions of arrays

pop- removes last an element of an array


$char= pop(@char);

push- adds an element at the end of an array


push(@char, joe);

shift- removes first an element of an array


$char= shift(@char);

unshift- adds an element at the beginning of


an array
unshift(@char, $char);
13

Lets write simple


Perl programs

14

Write Perl code for following


1.

To take input from user and print the


same.

2.

To print addition of two numbers entered


by user.

3.

To transcribe DNA into RNA.

4.

To open a text file and print file content.


15

1. Take input from user and


print the same.
print Enter text:;
chomp ($text= <STDIN>);
print You Entered-\t $text;
exit;
Output

16

2. Addition of two numbers


print "Enter first number:";
chomp ($fnum= <STDIN>);
print "Enter second number:";
chomp ($snum= <STDIN>);
$ans= $fnum + $snum;
print "Addition of two numbers $fnum & $snum
is $ans";
Output

17

3. Transcribing DNA to RNA


$DNA= "atgggctctccat";
$RNA= $DNA;
$RNA=~s/t/u/g;
print "Here is RNA\n";
print "$RNA";
exit;
Output

18

4. Program to open a text file


and print file content
$filename= myfile.txt';
open(FH, $filename);
@cont= <FH>;
close FH;
print @cont;
exit;
Output

19

Hashes

Also known as associative arrays

Hash names start with a %

a list of key/value pairs.

Example
%months = ('January' => 31,
...
'November' => 30,
'December' => 31);
20

Hashes- keys & values

Now all we need to do when we want to

know how many days are in November is


access the value in the %months hash

with a key of November:


print "Days in
November:",$months{'November'},"\n";
#Output November: 31
21

Write a program to find unique entries in


array.
@fruits=(banana, mango, apple, orange,
cherry,
mango, grapes,
orange);
%basket=();
foreach(@fruits)
{
$basket {$_}=1;
}
@unique= keys %basket;
#output
mango
print
@unique;
22

Resources For Perl

Books:
Learning Perl
By Rancho Wall
Published by O'Reilly

Programming Perl
By Rancho Wall, Tom Christiansen and Jon Orwant
Published by O'Reilly

Beginning Perl for Bioinformatics


By James D. Tisdall

The Complete Reference Perl


By Martin Brown

23

Web Sources for Perl

Web
www.perl.com
www.perldoc.org
www.perl.org
www.perlmonks.org

24

Questions???

25

You might also like