● perl reference

Post on 14-Dec-2015

224 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Perl tutorialPerl tutorialhttp://www.iki.fi/o/perltut

● Perl reference

http://www.rexswain.com/perl5.html

Perl tutorialPerl tutorial

What is Perl good for?

Perl tutorialPerl tutorial

What is Perl good for?● Small programs● Text file processing● Scripts

Perl tutorialPerl tutorial

Running programs

Perl tutorialPerl tutorial

Running programs● perl myprogram.perl● ./myprogram.perl

Perl tutorialPerl tutorial

Running programs

● ./myprogram.perl

#!/usr/bin/perl

chmod +x myprogram.perl

Perl tutorialPerl tutorial

Running programs● perl -e “print 1+2;”● echo “print 1+2;” | perl

Perl tutorialPerl tutorial

Running programs● cat inputdata.txt | ./myprogram.perl

Perl tutorialPerl tutorial

Running programs● cat inputdata.txt | ./myprogram.perl > outputdata.txt

Perl tutorialPerl tutorial

Running programs● ./myprogram.perl -i inputdata.txt -o outputdata.txt

Perl tutorialPerl tutorial

Programming● emacs myprogram.perl &

Perl tutorialPerl tutorial

Programming

#!/usr/local/bin/perl

print (“Hello world\n”);

Perl tutorialPerl tutorial

Programming

#!/usr/local/bin/perl

print (“Hello world\n”);

Perl tutorialPerl tutorial

Programming

#!/usr/local/bin/perl

print (“Hello world\n”);

Perl tutorialPerl tutorial

Programming

#!/usr/local/bin/perl

print (“Hello world\n”);

Perl tutorialPerl tutorial

Programming

#!/usr/local/bin/perl

print “Hello world\n”;

Perl tutorialPerl tutorial

Variables● Scalars● Lists/arrays● Hashes

Perl tutorialPerl tutorial

Variables● Scalars

$myvariable

Perl tutorialPerl tutorial

Variables● Scalars

$myvariable

Perl tutorialPerl tutorial

Variables● Scalars

$x = “100.000\n”;

print $x;

$x = $x + 1;

print $x;

Perl tutorialPerl tutorial

Variables● Scalars

$x = “100.000\n”; #string

print $x;

$x = $x + 1;

print $x;

Perl tutorialPerl tutorial

Variables● Scalars

$x = “100.000\n”;

print $x; #string

$x = $x + 1;

print $x;

Perl tutorialPerl tutorial

Variables● Scalars

$x = “100.000\n”;

print $x;

$x = $x + 1; #number

print $x;

Perl tutorialPerl tutorial

Variables● Scalars

$x = “100.000\n”;

print $x;

$x = $x + 1;

print $x; #string

Perl tutorialPerl tutorial

Variables● Lists/arrays

– @myarray– $myarray[0]– (1721, 2974, “blah”)– @myarray[0, 1, 2]– @myarray[0..2]

Perl tutorialPerl tutorial

Variables● Lists/arrays

@names = (“Adam”, “Eve”);

print($names[0].” likes “.$names[1]);

Perl tutorialPerl tutorial

Variables● Lists/arrays

– scalar(@myarray)– $myarray[$x][$y]– @sortedcopy = sort(@myarray)

Perl tutorialPerl tutorial

Variables● Hashes

– &myhash– $myhash{“blah”}– $myhash{$key1} = $value1;

Perl tutorialPerl tutorial

Variables● Hashes

$darker{“white”} = “grey”;

$darker{“grey”} = “black”;

print ($darker{“white”}. “ is darker than white.”);

Perl tutorialPerl tutorial

Variables● Hashes

– delete($myhash{$key});– ($key, $value) = each(%myhash);– @mykeys = keys(%myhash);– @myvalues = values(%myhash);

Perl tutorialPerl tutorial

Program flow● Blocks

{

statement1;

statement2;

}

Perl tutorialPerl tutorial

Program flow● Conditionals

if ($x == $y) {

#...

} elsif ($x == ($y+1)) {

#...

} else {

#...

}

Perl tutorialPerl tutorial

Program flow● Conditionals

– True

1, (“a”, “b”), “ “, “hello”, “00”– False

0, (), “”, “0”

Perl tutorialPerl tutorial

Program flow● Conditionals

Strings

eq

ne

lt

gt

Numbers

==

!=

<

>

Perl tutorialPerl tutorial

Program flow● Conditionals

&&

||

!

and

or

negation

Perl tutorialPerl tutorial

Program flow● Loops

for ($t = 0; $t < 100; $t++) {

#...

}

Perl tutorialPerl tutorial

Program flow● Loops

while ($x == $y) {

#...

}

Perl tutorialPerl tutorial

Program flow● Loops

do {

#...

} while ($x == $y);

Perl tutorialPerl tutorial

Program flow● Loops

foreach $key = keys(&myhash) {

#...

}

Perl tutorialPerl tutorial

Program flow● Loops

– last;– next;

Perl tutorialPerl tutorial

File handling● Handles

– STDIN– STDOUT

Perl tutorialPerl tutorial

File handling

open (INPUTFILE, “inputdata.txt”);

open (OUTPUTFILE, “>outputdata.txt”);

while ($line = <INPUTFILE>) {

print(OUTPUTFILE $line);

}

close(OUTPUTFILE);

close(INPUTFILE);

Perl tutorialPerl tutorial

File handling● Opening

“filename”

“<filename”

“>filename”

“>>filename”

“+>filename”

“| command”

“command |”

read

read

write, create

write, append

read, write

pipe to command

pipe from command

Perl tutorialPerl tutorial

File handling● Tests

if (-e “filename”) {

#File exists...

}

Perl tutorialPerl tutorial

File handling● Tests

-r

-x

-e

-d

-t

-T

readable

executable

exists

is a directory

is a tty

is a text file

Perl tutorialPerl tutorial

Command line arguments– @ARGV– scalar(@ARGV)

Perl tutorialPerl tutorial

Subroutines

sub a_plus_b {

exit($_[0] + $_[1]);

}

print(“1+2=“.&a_plus_b(1, 2));

Perl tutorialPerl tutorial

Typical Bugs● Mistyped identifiers● Forgetting $ etc.● Mixing strings and numbers● Forgetting that variables are global

Perl tutorialPerl tutorial

Additional features● Useful functions (mathematics, strings, etc.)

– ($a, $b) = split(“ “, “12 13”);● Regular expressions● UNIX system interaction● Networking● System VIPC (???)● Debugger

top related