1 / 49

Perl tutorial http://www.iki.fi/o/perltut

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

felicity
Télécharger la présentation

Perl tutorial http://www.iki.fi/o/perltut

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl tutorialhttp://www.iki.fi/o/perltut • Perl reference http://www.rexswain.com/perl5.html

  2. Perl tutorial What is Perl good for?

  3. Perl tutorial What is Perl good for? • Small programs • Text file processing • Scripts

  4. Perl tutorial Running programs

  5. Perl tutorial Running programs • perl myprogram.perl • ./myprogram.perl

  6. Perl tutorial Running programs • ./myprogram.perl #!/usr/bin/perl chmod +x myprogram.perl

  7. Perl tutorial Running programs • perl -e “print 1+2;” • echo “print 1+2;” | perl

  8. Perl tutorial Running programs • cat inputdata.txt | ./myprogram.perl

  9. Perl tutorial Running programs • cat inputdata.txt | ./myprogram.perl > outputdata.txt

  10. Perl tutorial Running programs • ./myprogram.perl -i inputdata.txt -o outputdata.txt

  11. Perl tutorial Programming • emacs myprogram.perl &

  12. Perl tutorial Programming #!/usr/local/bin/perl print (“Hello world\n”);

  13. Perl tutorial Programming #!/usr/local/bin/perl print (“Hello world\n”);

  14. Perl tutorial Programming #!/usr/local/bin/perl print (“Hello world\n”);

  15. Perl tutorial Programming #!/usr/local/bin/perl print (“Hello world\n”);

  16. Perl tutorial Programming #!/usr/local/bin/perl print “Hello world\n”;

  17. Perl tutorial Variables • Scalars • Lists/arrays • Hashes

  18. Perl tutorial Variables • Scalars $myvariable

  19. Perl tutorial Variables • Scalars $myvariable

  20. Perl tutorial Variables • Scalars $x = “100.000\n”; print $x; $x = $x + 1; print $x;

  21. Perl tutorial Variables • Scalars $x = “100.000\n”; #string print $x; $x = $x + 1; print $x;

  22. Perl tutorial Variables • Scalars $x = “100.000\n”; print $x; #string $x = $x + 1; print $x;

  23. Perl tutorial Variables • Scalars $x = “100.000\n”; print $x; $x = $x + 1; #number print $x;

  24. Perl tutorial Variables • Scalars $x = “100.000\n”; print $x; $x = $x + 1; print $x; #string

  25. Perl tutorial Variables • Lists/arrays • @myarray • $myarray[0] • (1721, 2974, “blah”) • @myarray[0, 1, 2] • @myarray[0..2]

  26. Perl tutorial Variables • Lists/arrays @names = (“Adam”, “Eve”); print($names[0].” likes “.$names[1]);

  27. Perl tutorial Variables • Lists/arrays • scalar(@myarray) • $myarray[$x][$y] • @sortedcopy = sort(@myarray)

  28. Perl tutorial Variables • Hashes • &myhash • $myhash{“blah”} • $myhash{$key1} = $value1;

  29. Perl tutorial Variables • Hashes $darker{“white”} = “grey”; $darker{“grey”} = “black”; print ($darker{“white”}. “ is darker than white.”);

  30. Perl tutorial Variables • Hashes • delete($myhash{$key}); • ($key, $value) = each(%myhash); • @mykeys = keys(%myhash); • @myvalues = values(%myhash);

  31. Perl tutorial Program flow • Blocks { statement1; statement2; }

  32. Perl tutorial Program flow • Conditionals if ($x == $y) { #... } elsif ($x == ($y+1)) { #... } else { #... }

  33. Perl tutorial Program flow • Conditionals • True 1, (“a”, “b”), “ “, “hello”, “00” • False 0, (), “”, “0”

  34. Numbers == != < > Perl tutorial Program flow • Conditionals Strings eq ne lt gt

  35. and or negation Perl tutorial Program flow • Conditionals && || !

  36. Perl tutorial Program flow • Loops for ($t = 0; $t < 100; $t++) { #... }

  37. Perl tutorial Program flow • Loops while ($x == $y) { #... }

  38. Perl tutorial Program flow • Loops do { #... } while ($x == $y);

  39. Perl tutorial Program flow • Loops foreach $key = keys(&myhash) { #... }

  40. Perl tutorial Program flow • Loops • last; • next;

  41. Perl tutorial File handling • Handles • STDIN • STDOUT

  42. Perl tutorial File handling open (INPUTFILE, “inputdata.txt”); open (OUTPUTFILE, “>outputdata.txt”); while ($line = <INPUTFILE>) { print(OUTPUTFILE $line); } close(OUTPUTFILE); close(INPUTFILE);

  43. 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 |”

  44. Perl tutorial File handling • Tests if (-e “filename”) { #File exists... }

  45. readable executable exists is a directory is a tty is a text file Perl tutorial File handling • Tests -r -x -e -d -t -T

  46. Perl tutorial Command line arguments • @ARGV • scalar(@ARGV)

  47. Perl tutorial Subroutines sub a_plus_b { exit($_[0] + $_[1]); } print(“1+2=“.&a_plus_b(1, 2));

  48. Perl tutorial Typical Bugs • Mistyped identifiers • Forgetting $ etc. • Mixing strings and numbers • Forgetting that variables are global

  49. Perl tutorial Additional features • Useful functions (mathematics, strings, etc.) • ($a, $b) = split(“ “, “12 13”); • Regular expressions • UNIX system interaction • Networking • System VIPC (???) • Debugger

More Related