1 / 59

Advanced UNIX

Advanced UNIX. 240-491 Special Topics in Comp. Eng. 1 Semester 2, 2000-2001. Objectives of these slides: introduce Perl (version 4.0-5.0) mostly based on Chapter 1, Learning Perl. 8. Introduction to Perl. Overview. 1. Starting with Perl 2. I/O 3. If-else 4. while 5. Arrays (lists).

nigel
Download Presentation

Advanced UNIX

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. Advanced UNIX 240-491 Special Topics in Comp. Eng. 1Semester 2, 2000-2001 • Objectives of these slides: • introduce Perl (version 4.0-5.0) • mostly based on Chapter 1, Learning Perl 8. Introduction to Perl

  2. Overview 1. Starting with Perl 2. I/O 3. If-else 4. while 5. Arrays (lists) continued

  3. 6. Handling Varying Input 7. Subroutines 8. File Processing 9. Reporting 10. Using the UNIX DBM Database 11. The Web and Perl

  4. 1. Starting with Perl • Practical Extraction and Report Language • Larry Wall, from 1987 • Features combine shell, awk, many UNIX tools, C libraries (all coded in Perl). • Emphasis on string manipulation, table generation, REs, excellent libraries • especially good Internet/Web libraries continued

  5. On many platforms; free • Many man and info pages. • Many newsgroups (e.g. comp.lang.perl) • Perl v.5 • OOP, more REs, modules • backward compatible with v.4 • check your version by typing: $ perl -v

  6. 1.1. Books • Learning PerlRandal L. Schwartz & Tom Christiansen O'Reilly, 1997, 2nd ed. • Programming PerlLarry Wall & Randal L. SchwartzO'Reilly, 2000, 3rd ed. • not easy to read, more of a reference text In our library

  7. 1.2. Web Resources • The main Perl site: http://www.perl.com • current Perl version, documentation, libraries (CPAN), FAQs, tools and utilities, features • reference sections on: • applications (e.g. games, graphics), Web, networking, system admin, etc. continued

  8. Resources about Perl • e.g. tutorials, manuals, Web-related, recommended books, example scripts • http://hem.fyristorg.com/gumby/ computing/perl.html • A beginner's Guide to Perl • http://home.bluemarble.net/~scotty/Perl/

  9. 1.3. Hello World (hw.pl) $ cat hw.pl#!/usr/bin/perlprint "Hello, world!\n";$ chmod u+x hw.pl$ hw.plHello, world! Or: $ perl hw.pl

  10. 2. I/O #!/usr/bin/perl# hm.plprint "What is your name? ";$name = <STDIN>;chop($name);print "Hello, $name!\n"; $ perl hm.pl What is your name? andrew Hello, andrew! $

  11. 3. if-else #!/usr/bin/perl# ch.plprint "What is your name? ";$name = <STDIN>;chop($name);if ($name eq "Randal") { print "Hello, Sir Randal!\n";} else { print "Hello, $name!\n"; # ordinary}

  12. 4. while Guess the secret word #!/usr/bin/perl# sword.pl$secretword = "llama";print "What is your name? ";$name = <STDIN>;chop($name);if ($name eq "Randal") { print "Hello, Sir Randal!\n";} : Not very secret continued

  13. else { print "Hello, $name!\n"; # ordinary print "Secret word? "; $guess = <STDIN>; chop($guess);while ($guess ne $secretword) { print "Wrong, try again: "; $guess = <STDIN>; chop($guess); }} $ perl sword.pl What is your name? andrew Hello, andrew! Secret word? foobar Wrong, try again: llama $

  14. 5. Arrays (lists) @words = ("camel", "llama", "oyster"); $words[0] is"camel" • List variables begin with @ • e.g. @words • $words[0] is a value in the list, so uses $

  15. swords.pl Guess the secret word (v.2) #!/usr/bin/perl@words = ("camel", "llama", "oyster"); print "What is your name? ";$name = <STDIN>;chop($name);if ($name eq "Randal") { print "Hello, Sir Randal!\n";} : An array of secret words continued

  16. else { print "Hello, $name!\n"; # ordinary print "Secret word? "; $guess = <STDIN>; chop($guess); $i = 0; # index into @words $correct = "maybe"; # dummy value : continued

  17. The user can win by guessing any of the secret words. while ($correct eq "maybe") { if ($words[$i] eq $guess) { # right? $correct = "yes"; } elsif ($i < 2) { # some words left? $i = $i + 1; } else { print "Wrong, try again: "; $guess = <STDIN>; chop($guess); $i = 0; # start words again }} }

  18. Usage $ perl swords.pl What is your name? andrew Hello, andrew! Secret word? foo Wrong, try again: bar Wrong, try again: llama $ one of the secret words

  19. 5.1. Associative Arrays (tables) • Person Secret Wordfred camelbarney llamabetty oysterwilma oyster • %words = ("fred", "camel", "barney","llama", "betty", "oyster", "wilma", "oyster"); • $words{"betty"}is "oyster" • Associative array variables begin with % • e.g. %words • $words{"betty"} is a value in the list, so uses $

  20. swords2.pl Each user has their own secret word #!/usr/bin/perl%words = ("fred","camel","barney","llama", "betty", "oyster", "wilma", "oyster");print "What is your name? ";$name = <STDIN>;chop($name);if ($name eq "Randal") { print "Hello, Sir Randal!\n";} : continued

  21. else { print "Hello, $name!\n"; # ordinary $secretword = $words{$name}; # get secret word print "Secret word? "; $guess = <STDIN>; chop($guess); while ($guess ne $secretword) { print "Wrong, try again: "; $guess = <STDIN>; chop($guess); }}

  22. Usage $ perl swords2.pl What is your name? barney Hello, barney! Secret word? oyster Wrong, try again: foobar Wrong, try again: llama $ barney's secret word!

  23. 5.2. Adding a Default Secret Word ... $secretword = $words{$name}; if ($secretword eq "") { # not found! $secretword = "groucho"; } print "Secret word? ";...

  24. 6. Handling Varying Input • sword.pl (and others) treats "Randal" in a special way. • But: "Randal L. Schwartz"or "randal"is treated like other users.

  25. 6.1. Replace eq by =~ and RE REs are one of the big advantages of Perl ...if ($name =~ /^Randal/) { # it matches, do something} else { # no match, do something else}...

  26. 6.2. Extended REs • Check for a word boundary with \b • Ignore case with the i option (after the last /)

  27. Part of final1.pl ...if ($name =~ /^randal\b/i) { # matches "Randal...", "ranDAL..." print "Hello, Sir Randal!\n";} else { # no match, do something else}...

  28. 6.3. Substitution and Translation • Substitution: find and replace(like s/../../ in vi). • Translation: map characters to others(like tr)

  29. ...print "What is your name? ";$name = <STDIN>;chop($name);$name =~ s/\W.*//; # Get rid of everything # after first word.$name =~ tr/A-Z/a-z/; # Make lowercase.if ($name eq "randal") { print "Hello, Sir Randal!\n"; }... e.g. Dr.Andrew Davison becomes dr

  30. 7. Subroutines Not a good feature sub good_word { # used in final1.pllocal($somename, $someguess) = @_; # input arguments $somename =~ s/\W.*//; $somename =~ tr/A-Z/a-z/; if ($somename eq "randal") 1 # return true } elsif (($words{$somename} || "groucho") eq $someguess) { 1; } else { 0; # return false }} # end of subroutine

  31. Called in final1.pl: ... print "Secret word? "; $guess = <STDIN>; chop($guess); while (! &good_word($name, $guess) ) { print "Wrong, try again: "; $guess = <STDIN>; chop($guess); }} Don't forget the &

  32. 8. File Processing • Read words from the "wordslist" file: sub init_words { # used inside final1.plopen(WORDSLIST, "wordslist"); while ($name = <WORDSLIST>) { chop($name); $word = <WORDSLIST>; chop($word); $words{$name} = $word; }close(WORDSLIST);}

  33. wordslist Format fredcamelbarneyllamabettyoysterwilmaoyster The code treats the file as four pairs: name secret-word

  34. Called in final1.pl: #!/usr/bin/perl&init_words;print "What is your name? ";$name = <STDIN>;chop($name);...

  35. 8.1. File Tests -M is the file's modification date sub init_words { # second version open(WORDSLIST, "wordslist");if (-M WORDSLIST > 7) { die "Sorry, wordslist is too old"; } while ($name = <WORDSLIST>) { chop($name); $word = <WORDSLIST>; chop($word); $words{$name} = $word; } close(WORDSLIST);}

  36. 8.2. The open() Command • Similar to popen() in C • the command argument is executed and can be written to via an output stream • In good_word: ...open(MAIL, "| mail dandrew@ratree.psu.ac.th");print MAIL "bad news $somename guessed $someguess\n";close(MAIL);... mail MAIL

  37. 8.3. Filename Expansion Also called filename globbing • Assume several files of secret words: • ad.secret, yuk.secret • Modify init_words() to read in all of these • but only if the files are less than 7 days old • Store in the %words associative array

  38. filename expansion sub init_words { # third version while ($filename = <*.secret>) { open(WORDSLIST, $filename); if (-M WORDSLIST < 7) { while ($name = <WORDSLIST>) { chop($name); $word = <WORDSLIST>; chop($word); $words($name) = $word; } } close(WORDSLIST); }}

  39. 8.4. The Password File • A typical password line: ad:x:42497:100:Dr.Andrew DAVISON,,,: /home/ad:/bin/bash • the user's details are in the 5th field • the GECOS field • $< always contains the user's ID number (UID)

  40. There is a getpwuid() in C which does the same thing #!/usr/bin/perl# final1.pl&init_words;@password = getpwuid( $< ); # get password data using the UID$name = $password[6]; # get the GECOS field (6th in Perl!)$name =~ s/,.*//; # throw away everything # after 1st comma if ($name =~ /^randal\b/i) { print "Hello, Sir Randal!\n";}...

  41. 9. Reporting • secret.pl: • list the *.secret files in the format:filename user-name secret-word

  42. secret.pl #!/usr/bin/perlwhile ($filename = <*.secret>) { open(WORDSLIST, $filename); if (-M WORDSLIST < 7) { while ($name = <WORDSLIST>) { chop($name); $word = <WORDSLIST>; chop($word);write;# invoke STDOUT format } } close(WORDSLIST);} continued

  43. STDOUT Format Definition format STDOUT =@<<<<<<<< @<<<<<<<<< @<<<<<<<<<<<$filename, $name, $word. field definition line field value line continued

  44. Top of Page Format • Used every 60 lines by default: format STDOUT_TOP =Page @<< # field definition line$% # number of pages printedFilename Name Word=========== ======= ========.

  45. Output The *.secret files may need to be 'touched'. $ secret.plPage 1Filename Name Word============= ======= ========ad.secret andrew f1ad.secret paul f2ad.secret dr foobaryuk.secret jim c1yuk.secret bob c2$

  46. 10. Using the UNIX DBM Database • In final1.pl, the time of the most recent correct guess is stored in %last_good $last_good{$name} = time; • Problem: %last_good will be lost when the script terminates. • Solution: store %last_good in a DBM database.

  47. Revised final1.pl rw for everyone ...dbmopen(%last_good, "lastdb", 0666);$last_good{$name} = time;dbmclose(%last_good);... returns time in seconds since 1/1/1970

  48. Using final1.pl • $ final1.plHello, Dr.Andrew DAVISON!What is the secret word? bingoWrong, try again. What is the secret word? foobar$ foobar is the secret word for dr (see slide 45)

  49. List the Last Guessses (ex_last.pl) #!/usr/bin/perldbmopen(%last_good,"lastdb",0666);foreach $name (sortkeys(%last_good)) { $when = $last_good{$name}; $hours = (time - $when) / 3600; # compute hours ago write; # use STDOUT format}format STDOUT =User @<<<<<<<<<<<<: last correct guess was @<<< hours ago.$name, $hours.

  50. Output last year, when my user details were different $ ex_last.plUser Andrew DAVIS: last correct guess was 2066 hours ago.User Dr.Andrew DA: last correct guess was 0.07 hours ago.$

More Related