● Perl reference .
-
Upload
laila-trippe -
Category
Documents
-
view
222 -
download
3
Transcript of ● Perl reference .
![Page 1: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/1.jpg)
Perl tutorialPerl tutorialhttp://www.iki.fi/o/perltut
● Perl reference
http://www.rexswain.com/perl5.html
![Page 2: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/2.jpg)
Perl tutorialPerl tutorial
What is Perl good for?
![Page 3: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/3.jpg)
Perl tutorialPerl tutorial
What is Perl good for?● Small programs● Text file processing● Scripts
![Page 4: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/4.jpg)
Perl tutorialPerl tutorial
Running programs
![Page 5: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/5.jpg)
Perl tutorialPerl tutorial
Running programs● perl myprogram.perl● ./myprogram.perl
![Page 6: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/6.jpg)
Perl tutorialPerl tutorial
Running programs
● ./myprogram.perl
#!/usr/bin/perl
chmod +x myprogram.perl
![Page 7: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/7.jpg)
Perl tutorialPerl tutorial
Running programs● perl -e “print 1+2;”● echo “print 1+2;” | perl
![Page 8: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/8.jpg)
Perl tutorialPerl tutorial
Running programs● cat inputdata.txt | ./myprogram.perl
![Page 9: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/9.jpg)
Perl tutorialPerl tutorial
Running programs● cat inputdata.txt | ./myprogram.perl > outputdata.txt
![Page 10: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/10.jpg)
Perl tutorialPerl tutorial
Running programs● ./myprogram.perl -i inputdata.txt -o outputdata.txt
![Page 11: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/11.jpg)
Perl tutorialPerl tutorial
Programming● emacs myprogram.perl &
![Page 12: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/12.jpg)
Perl tutorialPerl tutorial
Programming
#!/usr/local/bin/perl
print (“Hello world\n”);
![Page 13: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/13.jpg)
Perl tutorialPerl tutorial
Programming
#!/usr/local/bin/perl
print (“Hello world\n”);
![Page 14: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/14.jpg)
Perl tutorialPerl tutorial
Programming
#!/usr/local/bin/perl
print (“Hello world\n”);
![Page 15: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/15.jpg)
Perl tutorialPerl tutorial
Programming
#!/usr/local/bin/perl
print (“Hello world\n”);
![Page 16: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/16.jpg)
Perl tutorialPerl tutorial
Programming
#!/usr/local/bin/perl
print “Hello world\n”;
![Page 17: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/17.jpg)
Perl tutorialPerl tutorial
Variables● Scalars● Lists/arrays● Hashes
![Page 18: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/18.jpg)
Perl tutorialPerl tutorial
Variables● Scalars
$myvariable
![Page 19: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/19.jpg)
Perl tutorialPerl tutorial
Variables● Scalars
$myvariable
![Page 20: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/20.jpg)
Perl tutorialPerl tutorial
Variables● Scalars
$x = “100.000\n”;
print $x;
$x = $x + 1;
print $x;
![Page 21: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/21.jpg)
Perl tutorialPerl tutorial
Variables● Scalars
$x = “100.000\n”; #string
print $x;
$x = $x + 1;
print $x;
![Page 22: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/22.jpg)
Perl tutorialPerl tutorial
Variables● Scalars
$x = “100.000\n”;
print $x; #string
$x = $x + 1;
print $x;
![Page 23: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/23.jpg)
Perl tutorialPerl tutorial
Variables● Scalars
$x = “100.000\n”;
print $x;
$x = $x + 1; #number
print $x;
![Page 24: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/24.jpg)
Perl tutorialPerl tutorial
Variables● Scalars
$x = “100.000\n”;
print $x;
$x = $x + 1;
print $x; #string
![Page 25: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/25.jpg)
Perl tutorialPerl tutorial
Variables● Lists/arrays
– @myarray– $myarray[0]– (1721, 2974, “blah”)– @myarray[0, 1, 2]– @myarray[0..2]
![Page 26: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/26.jpg)
Perl tutorialPerl tutorial
Variables● Lists/arrays
@names = (“Adam”, “Eve”);
print($names[0].” likes “.$names[1]);
![Page 27: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/27.jpg)
Perl tutorialPerl tutorial
Variables● Lists/arrays
– scalar(@myarray)– $myarray[$x][$y]– @sortedcopy = sort(@myarray)
![Page 28: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/28.jpg)
Perl tutorialPerl tutorial
Variables● Hashes
– &myhash– $myhash{“blah”}– $myhash{$key1} = $value1;
![Page 29: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/29.jpg)
Perl tutorialPerl tutorial
Variables● Hashes
$darker{“white”} = “grey”;
$darker{“grey”} = “black”;
print ($darker{“white”}. “ is darker than white.”);
![Page 30: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/30.jpg)
Perl tutorialPerl tutorial
Variables● Hashes
– delete($myhash{$key});– ($key, $value) = each(%myhash);– @mykeys = keys(%myhash);– @myvalues = values(%myhash);
![Page 31: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/31.jpg)
Perl tutorialPerl tutorial
Program flow● Blocks
{
statement1;
statement2;
}
![Page 32: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/32.jpg)
Perl tutorialPerl tutorial
Program flow● Conditionals
if ($x == $y) {
#...
} elsif ($x == ($y+1)) {
#...
} else {
#...
}
![Page 33: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/33.jpg)
Perl tutorialPerl tutorial
Program flow● Conditionals
– True
1, (“a”, “b”), “ “, “hello”, “00”– False
0, (), “”, “0”
![Page 34: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/34.jpg)
Perl tutorialPerl tutorial
Program flow● Conditionals
Strings
eq
ne
lt
gt
Numbers
==
!=
<
>
![Page 35: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/35.jpg)
Perl tutorialPerl tutorial
Program flow● Conditionals
&&
||
!
and
or
negation
![Page 36: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/36.jpg)
Perl tutorialPerl tutorial
Program flow● Loops
for ($t = 0; $t < 100; $t++) {
#...
}
![Page 37: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/37.jpg)
Perl tutorialPerl tutorial
Program flow● Loops
while ($x == $y) {
#...
}
![Page 38: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/38.jpg)
Perl tutorialPerl tutorial
Program flow● Loops
do {
#...
} while ($x == $y);
![Page 39: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/39.jpg)
Perl tutorialPerl tutorial
Program flow● Loops
foreach $key = keys(&myhash) {
#...
}
![Page 40: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/40.jpg)
Perl tutorialPerl tutorial
Program flow● Loops
– last;– next;
![Page 41: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/41.jpg)
Perl tutorialPerl tutorial
File handling● Handles
– STDIN– STDOUT
![Page 42: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/42.jpg)
Perl tutorialPerl tutorial
File handling
open (INPUTFILE, “inputdata.txt”);
open (OUTPUTFILE, “>outputdata.txt”);
while ($line = <INPUTFILE>) {
print(OUTPUTFILE $line);
}
close(OUTPUTFILE);
close(INPUTFILE);
![Page 43: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/43.jpg)
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
![Page 44: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/44.jpg)
Perl tutorialPerl tutorial
File handling● Tests
if (-e “filename”) {
#File exists...
}
![Page 45: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/45.jpg)
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
![Page 46: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/46.jpg)
Perl tutorialPerl tutorial
Command line arguments– @ARGV– scalar(@ARGV)
![Page 47: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/47.jpg)
Perl tutorialPerl tutorial
Subroutines
sub a_plus_b {
exit($_[0] + $_[1]);
}
print(“1+2=“.&a_plus_b(1, 2));
![Page 48: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/48.jpg)
Perl tutorialPerl tutorial
Typical Bugs● Mistyped identifiers● Forgetting $ etc.● Mixing strings and numbers● Forgetting that variables are global
![Page 49: ● Perl reference .](https://reader035.fdocuments.us/reader035/viewer/2022062407/56649c885503460f949403b1/html5/thumbnails/49.jpg)
Perl tutorialPerl tutorial
Additional features● Useful functions (mathematics, strings, etc.)
– ($a, $b) = split(“ “, “12 13”);● Regular expressions● UNIX system interaction● Networking● System VIPC (???)● Debugger