490 likes | 713 Views
Perl tutorial http://www.iki.fi/o/perltut. Perl reference http://www.rexswain.com/perl5.html. Perl tutorial. What is Perl good for?. Perl tutorial. What is Perl good for? Small programs Text file processing Scripts. Perl tutorial. Running programs. Perl tutorial. Running programs
E N D
Perl tutorialhttp://www.iki.fi/o/perltut • Perl reference http://www.rexswain.com/perl5.html
Perl tutorial What is Perl good for?
Perl tutorial What is Perl good for? • Small programs • Text file processing • Scripts
Perl tutorial Running programs
Perl tutorial Running programs • perl myprogram.perl • ./myprogram.perl
Perl tutorial Running programs • ./myprogram.perl #!/usr/bin/perl chmod +x myprogram.perl
Perl tutorial Running programs • perl -e “print 1+2;” • echo “print 1+2;” | perl
Perl tutorial Running programs • cat inputdata.txt | ./myprogram.perl
Perl tutorial Running programs • cat inputdata.txt | ./myprogram.perl > outputdata.txt
Perl tutorial Running programs • ./myprogram.perl -i inputdata.txt -o outputdata.txt
Perl tutorial Programming • emacs myprogram.perl &
Perl tutorial Programming #!/usr/local/bin/perl print (“Hello world\n”);
Perl tutorial Programming #!/usr/local/bin/perl print (“Hello world\n”);
Perl tutorial Programming #!/usr/local/bin/perl print (“Hello world\n”);
Perl tutorial Programming #!/usr/local/bin/perl print (“Hello world\n”);
Perl tutorial Programming #!/usr/local/bin/perl print “Hello world\n”;
Perl tutorial Variables • Scalars • Lists/arrays • Hashes
Perl tutorial Variables • Scalars $myvariable
Perl tutorial Variables • Scalars $myvariable
Perl tutorial Variables • Scalars $x = “100.000\n”; print $x; $x = $x + 1; print $x;
Perl tutorial Variables • Scalars $x = “100.000\n”; #string print $x; $x = $x + 1; print $x;
Perl tutorial Variables • Scalars $x = “100.000\n”; print $x; #string $x = $x + 1; print $x;
Perl tutorial Variables • Scalars $x = “100.000\n”; print $x; $x = $x + 1; #number print $x;
Perl tutorial Variables • Scalars $x = “100.000\n”; print $x; $x = $x + 1; print $x; #string
Perl tutorial Variables • Lists/arrays • @myarray • $myarray[0] • (1721, 2974, “blah”) • @myarray[0, 1, 2] • @myarray[0..2]
Perl tutorial Variables • Lists/arrays @names = (“Adam”, “Eve”); print($names[0].” likes “.$names[1]);
Perl tutorial Variables • Lists/arrays • scalar(@myarray) • $myarray[$x][$y] • @sortedcopy = sort(@myarray)
Perl tutorial Variables • Hashes • &myhash • $myhash{“blah”} • $myhash{$key1} = $value1;
Perl tutorial Variables • Hashes $darker{“white”} = “grey”; $darker{“grey”} = “black”; print ($darker{“white”}. “ is darker than white.”);
Perl tutorial Variables • Hashes • delete($myhash{$key}); • ($key, $value) = each(%myhash); • @mykeys = keys(%myhash); • @myvalues = values(%myhash);
Perl tutorial Program flow • Blocks { statement1; statement2; }
Perl tutorial Program flow • Conditionals if ($x == $y) { #... } elsif ($x == ($y+1)) { #... } else { #... }
Perl tutorial Program flow • Conditionals • True 1, (“a”, “b”), “ “, “hello”, “00” • False 0, (), “”, “0”
Numbers == != < > Perl tutorial Program flow • Conditionals Strings eq ne lt gt
and or negation Perl tutorial Program flow • Conditionals && || !
Perl tutorial Program flow • Loops for ($t = 0; $t < 100; $t++) { #... }
Perl tutorial Program flow • Loops while ($x == $y) { #... }
Perl tutorial Program flow • Loops do { #... } while ($x == $y);
Perl tutorial Program flow • Loops foreach $key = keys(&myhash) { #... }
Perl tutorial Program flow • Loops • last; • next;
Perl tutorial File handling • Handles • STDIN • STDOUT
Perl tutorial File handling open (INPUTFILE, “inputdata.txt”); open (OUTPUTFILE, “>outputdata.txt”); while ($line = <INPUTFILE>) { print(OUTPUTFILE $line); } close(OUTPUTFILE); close(INPUTFILE);
read read write, create write, append read, write pipe to command pipe from command Perl tutorial File handling • Opening “filename” “<filename” “>filename” “>>filename” “+>filename” “| command” “command |”
Perl tutorial File handling • Tests if (-e “filename”) { #File exists... }
readable executable exists is a directory is a tty is a text file Perl tutorial File handling • Tests -r -x -e -d -t -T
Perl tutorial Command line arguments • @ARGV • scalar(@ARGV)
Perl tutorial Subroutines sub a_plus_b { exit($_[0] + $_[1]); } print(“1+2=“.&a_plus_b(1, 2));
Perl tutorial Typical Bugs • Mistyped identifiers • Forgetting $ etc. • Mixing strings and numbers • Forgetting that variables are global
Perl tutorial Additional features • Useful functions (mathematics, strings, etc.) • ($a, $b) = split(“ “, “12 13”); • Regular expressions • UNIX system interaction • Networking • System VIPC (???) • Debugger