welcome to cs 368
play

Welcome to CS 368! Introductions, Overview, Course Mechanics, - PowerPoint PPT Presentation

Computer Sciences 368 Introduction to Perl Welcome to CS 368! Introductions, Overview, Course Mechanics, Resources, etc. 2012 Summer Cartwright 1 Computer Sciences 368 Introduction to Perl Introductions 2012 Summer Cartwright 2


  1. Computer Sciences 368 Introduction to Perl Welcome to CS 368! Introductions, Overview, Course Mechanics, Resources, etc. 2012 Summer Cartwright 1

  2. Computer Sciences 368 Introduction to Perl Introductions 2012 Summer Cartwright 2

  3. Computer Sciences 368 Introduction to Perl Introduction to Perl Tim Cartwright 2012 Summer Cartwright 3

  4. Computer Sciences 368 Introduction to Perl Website http://pages.cs.wisc.edu/~cs368-3/ 2012 Summer Cartwright 4

  5. Computer Sciences 368 Introduction to Perl Brief Overview 2012 Summer Cartwright 5

  6. Computer Sciences 368 Introduction to Perl Course Objectives • Write basic code in Perl • Solve real-world problems with scripting languages • Learn about scripting languages (Perl, Python, Ruby, PHP, JavaScript, ActionScript, … ) 2012 Summer Cartwright 6

  7. Computer Sciences 368 Introduction to Perl Scripting • Fast development • Easy to understand and change • Abstracts over low-level details • Pervasive • Examples: – quick (to write) computation – data manipulation – glue – e.g., SpamAssassin, Twitter (Ruby on Rails) 2012 Summer Cartwright 7

  8. Computer Sciences 368 Introduction to Perl Perl • Practical Extraction and Report Language (Pathologically Eclectic Rubbish Lister?) • Introduced 1987 – Perl 5.8: 2002–2008 – Perl 5.10: 2007–2009 version used in class! – … – Perl 5.16: imminent • Widely available • Great for processing text • Huge number of independent libraries (CPAN) 2012 Summer Cartwright 8

  9. Computer Sciences 368 Introduction to Perl Course Philosophy Learn a new skill Learn by doing Learn to fish 2012 Summer Cartwright 9

  10. Computer Sciences 368 Introduction to Perl Course Mechanics 2012 Summer Cartwright 10

  11. Computer Sciences 368 Introduction to Perl Homework and Grading • Credit – Course o ff ered as credit/no credit – All points come from homework (no exam) • Homework – Short coding assignment – Every day (except last day): 15 total – Due by 11:10 a.m. of next class (email tolerated) – No late assignments accepted at all – Each assignment given 0, 1, or 2 points – Need 20 points (67%) to get credit for the course – No extra points available 2012 Summer Cartwright 11

  12. Computer Sciences 368 Introduction to Perl Homework Points Pts Reason • turned in on time, AND • code runs, AND 2 • solution is correct or nearly so, AND • demonstrates real e ff ort • turned in on time, AND 1 • partial solution, may not actually run, AND • demonstrates some e ff ort (my discretion) • late, OR 0 • is plagiarized (= Academic Miscondict) , OR • does not demonstrate any real e ff ort 2012 Summer Cartwright 12

  13. Computer Sciences 368 Introduction to Perl Mailing List compsci368-3-su12-dhh@lists.wisc.edu • Goes to @wisc.edu account • Check spam filters 2012 Summer Cartwright 13

  14. Computer Sciences 368 Introduction to Perl O ffi ce Hours Computer Sciences 4265 (Tim’s o ffi ce) Days and times: Doodle poll today! Other times available by appointment (email) 2012 Summer Cartwright 14

  15. Computer Sciences 368 Introduction to Perl Course Books • Learning Perl (6th Ed.) • Perl Cookbook (2nd Ed.) • Programming Perl (3rd Ed.) • Available FREE online via MadCat • Not in the UBS textbook area • If you buy them, consider newest editions 2012 Summer Cartwright 15

  16. Computer Sciences 368 Introduction to Perl Some Perl Examples 2012 Summer Cartwright 16

  17. Computer Sciences 368 Introduction to Perl List and Count Directory Entries use File::Basename; my $entry = $ARGV[0]; print join("\n", glob("$entry/{.,?}*")) . "\n"; print "Number of entries in $entry: "; print scalar(grep { basename($_) !~ /^\.\.?$/ } glob("$entry/{.,?}*")); print "\n"; 2012 Summer Cartwright 17

  18. Computer Sciences 368 Introduction to Perl Average Numbers in Data File Columns my $count = 0; my @s; while (<STDIN>) { my @w = split; $count++; for (my $i = 0; $i <= $#w; $i++) { $s[$i] += $w[$i]; } } for (my $i = 0; $i <= $#w; $i++) { print $s[$i] / $count, "\t"; } print "\n"; 2012 Summer Cartwright 18

  19. Computer Sciences 368 Introduction to Perl Run a System Command As Another User use POSIX "setsid"; my $daemon; if ($ARGV[0] eq "--detach") { shift @ARGV; $daemon = 1; } my ($user, @cmd) = @ARGV; die "Can only be run by root\n" if $<; die "User $user does not exist\n" unless getpwnam($user); my $new_primary_group = (getpwnam($user))[3]; my $new_secondary_groups = `id -G $user`; if ($? ne 0) { $new_secondary_groups = $new_primary_group; } $( = $) = "$new_primary_group $new_secondary_groups"; $< = $> = (getpwnam($user))[2]; if ($daemon) { exit if fork; setsid; STDIN->open("/dev/null"); STDOUT->open(">>/dev/null"); STDERR->open(">>/dev/null"); } exec(@cmd); exit(1); 2012 Summer Cartwright 19

  20. Computer Sciences 368 Introduction to Perl How to Run Perl 2012 Summer Cartwright 20

  21. Computer Sciences 368 Introduction to Perl Running Perl • Unix/Linux – perl filename – chmod 0755 filename ./ filename • Mac OS X – use Terminal, same as above • Windows – download ActiveState Perl – not o ffi cially supported in the course 2012 Summer Cartwright 21

  22. Computer Sciences 368 Introduction to Perl Some Basic Syntax 2012 Summer Cartwright 22

  23. Computer Sciences 368 Introduction to Perl Hello World #!/usr/bin/perl use strict; use warnings; # Everyone's first Perl program print "Hello, world!\n"; 2012 Summer Cartwright 23

  24. Computer Sciences 368 Introduction to Perl Numbers & Math • literals: 42 , 3.141 , –6.5e9 , 0377 , 0xff • operators: + – * / ** % ( ) 4 + 7 => 11 17.8 – 3.5 => 14.3 16 * 0x10 => 256 2 ** 8 => 256 10 / 3 => 3.333333… 10 % 3 => 1 (2 + 3) * 4 => 20 2012 Summer Cartwright 24

  25. Computer Sciences 368 Introduction to Perl Homework for Day 1 • Go to section website • Find syllabus • Find and read homework • Run the homework script on your own machine and print the output • Turn in output tomorrow 2012 Summer Cartwright 25

  26. Computer Sciences 368 Introduction to Perl http://pages.cs.wisc.edu/~cs368-3/ 2012 Summer Cartwright 26

Recommend


More recommend