Vous êtes sur la page 1sur 25

By: Fateme Karimi Sara Mohammadi

1387/09/23

Generic view

History Features Syntax Date objects Arrays Loops & conditional exprs Operators Subroutines Functions Regular Expressions I/O and file handles Applications And at the end.. refrences
2/25

History

Was created in 1986 by Larry Wall in the Jet Nasa Lab

SH
SED

C Perl
Lis p

AW K

Ruby

PHP

3/25

(perl)
5.10.0 5.8.2 5.8.0 5.7.0 5.6.0 5.000

history(cont.)

.
. . .

. .
.

26.0ct,1995

4.000

The Comprehensive Perl Archive Network

3.000

2.000

1.000

.
Dec.18,1987

.
July.18,2002 March.21,1991 March.28,2000 July.26,1998 Oct.18,1989 Dec.18,2007 Feb.21,1988 Oct.18,1994 Oct.23,2003

(T)

(Perl Time)

4/25

Features
Perl: is flexible and easy to learn is functional and interpreted language is Open Source software is powerfull string manipulation language supports OOP is portable language have automatic memory management uses dynamic typing
5/25

Syntax
Things are different shoud look different. #!/usr/bin/perl
Path to the Perl interpreter

#This is my first

program

Comments

print Perl is my friend\n;


For output All statements must be have ; in end

\n, \t, \b,\l, \u, \L, \U, \E


PrintAB \l CD; #get ABcD
6/25

Printab\Ucd\Ee; #get abCDe

Data objects
Elementry
int real char string

Data structures

Scalar arrays Associative array

Numbers 7, 3.145, -0.004, 3.40E15

Perl supports scientific notation


output

$num = 5; print your number is $num.; print your number is $num.;

your number is $num. your number is 5.

$str = FIRST; $str = 345;

FIRST
str 345
7/25

Arrays
Scalar

array
@array = (4, 5, 6, 7);

@array2 = (A..J);
push(@array, 8); pop(@array);
Acssec

0 1 2 3 4

to array
# $element = 4

$element = $array[0];
the @ changes to a $ because elements are scalars

8/25

Associative

arrays

%lookup = (peter, 123, andrew, 456,

jack, 789);

Print $lookup{peter};

# get 123

@name = keys(%lookup);

peter
123

andrew jack
456 789

@code = values(%lookup);
Delete %lookup(jack);
Delete key & value

9/25

Loops & conditional exprs


while
foreac h
loop

for

if

conditional

until

$i = 1; until ( $i > 10) { # count to 10 print $i \n; ++$i; }

@array = (2,4,6,8); foreach $element (@array) { $element++; print $element, \n; }

3 5 7 9

10/25

Operators
arithmetic ** *, / +, % ++, -Logical && || ! assignmen t **= *=, /= +=, -= %= >>=, <<= ^= ~(10010) ; #The COMPLEMENT op1>> op2; 128 >> 3; # output 1024 ~ bitwise << >> & ^ (xor) |
(negation)

01101

11/25

Operators(cont.
)
Comparison operators Equal numerical == string eq

Not equal comparison


relational operators Less than Grater than Less than or equal Grater than or equal

!= <=>
numerical < > <= >=

ne cmp
string lt gt le ge

<=> returns -1, 0, or 1

$low = 8; $hi = 10; Print ($low <=> $hi, \n ); # get -1


12/25

Subroutines
sub tells the interpreter you are declaring a function function name to call the function

sub area{ ($height, $width) = @_;


This is an array that gets created automatically to hold the parameter list.

local($num) = 50;
$area = $height * $width; printheight = $height.\n width = $width.\n area is $area.;

local is a variable qualifier that makes it local to the function.

area(4,3); #output: height = 4. width = 3. area is 12.

13/25

Subroutines(cont.)
Sub first {

first(1,2,3); first(1..4); first(A..Z);

$parametrs = @_; printnumber of parametrs is $parametrs.\n;


}

number of parametrs is 3. number of parametrs is 4. number of parametrs is 26.

14/25

Functions
string

functions
lc(STRING);
lc(ABC); # output is abc

$var Substr(STRING, OFFSET, LENGHT); = 1234bb789; substr($var, 4, 2) = aa; print var = $var; # output is 1234aa789

index(STRING, SUBSTRING);
index(I am a student, am); # return 2 index(I am a student, book); # return -1

15/25

Functions(cont.)
array

function
@f = sort(small, medium, large); # gets @f = (large, medium, large)

sort(ARRAY);

reverse(ARRAY) ;

@arry = (4,5,6,7); @b = Reverse(@arry); # @b = (7, 6, 5, 4)

split(PATTERN, STRING);

$line = jack, piter, tom, bob; @x = spilt(/, /, $line );


jack piter tom bob

0 1 2 3
16/25

Regular Expressions
=~ & !~are important operators we use with REGEX
The pattern is a set of characters between //

$str = AGATGATAT; if ($str =~ m/ATG/) { print Match!;}

=~ comparison operator

# output is Match!

17/25

REGEX - Substitution
Substitution operator

$str = AGATGATAT; $str =~ s/T/U/g;

Pattern to search for

print $str\n;

Replacement string
Global replacement

#get AGAUGAUAU
Substitution changes the variable

18/2 5

REGEX - Translation
Translation operator

$str = AGATGATAT; $str =~ tr/ACGT/TGCA/; print $str, \n;

Set of characters to replace

Replacement characters

# get TCTACTATA

Translation changes the variable

19/25

I/O and file handles

Most data of any size is stored on computer as files Filehandles allow you to read a file from disc and store the information in memory
For output

predefined file handles: STDIN, STDOUT, STDERR


For input

Filehandles have 3 major operations

open read one line at a time with <> close

20/2 5

File(example)
open(HANDLE, "< /file/path"); #open for reading open(HANDLE, "> /file/path"); #open for writing open(HANDLE, ">> /file/path"); # open for appending
<HANDLE>;
Reads 1line from Open file

close(HANDLE);
open(HPSW, > /SYS/PUB/HPSWINFO"); # open for input $one = <HPSW>; # read first line $two = <HPSW>; # read second line @therest = <HPSW>; # read all remaining lines close(HPSW); # close the file 21/2 5

Perl applications
It

uses in AI, genetic, industry, internet. Its one of the three Ps.(alonge with Python and PHP) Is very powerfull in 3D programming. It used as a glue language. works with XML and HTML. Its good network programming language. With perl you can even write a OS!!

22/25

And at the end..


Perl takes associative array from AWK, regular expression from sed, and very features from C. Perl can uses in more than 40 OS. Perl says: easy things shoud be easy and hard things shoud be possible! Perl eases programmers task at the expensive of CPU & memory requiremens. Perl Intends to be practical rather than beautiful.
23/2 5

References
Related links
Pratt, Trans W. /zelkowits, http://www.perl.com

Marvin V./programming language: design http://wikipedia.org and implementation/salkhorde, m.mahdi/khorasan publications. http://www.PRDEV.com http://billhails.net http://www.aj.com

24/25

25/2 5

Vous aimerez peut-être aussi