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

welcome to cs 368
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Welcome to CS 368!

Introductions, Overview, Course Mechanics, Resources, etc.

1

slide-2
SLIDE 2

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Introductions

2

slide-3
SLIDE 3

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Introduction to Perl

Tim Cartwright

3

slide-4
SLIDE 4

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Website

http://pages.cs.wisc.edu/~cs368-3/

4

slide-5
SLIDE 5

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

5

Brief Overview

slide-6
SLIDE 6

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

  • Write basic code in Perl
  • Solve real-world problems with scripting languages
  • Learn about scripting languages

(Perl, Python, Ruby, PHP, JavaScript, ActionScript, …)

6

Course Objectives

slide-7
SLIDE 7

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

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)

7

slide-8
SLIDE 8

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

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)

8

slide-9
SLIDE 9

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Course Philosophy

9

Learn a new skill Learn by doing Learn to fish

slide-10
SLIDE 10

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Course Mechanics

10

slide-11
SLIDE 11

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Homework and Grading

  • Credit

– Course offered 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

11

slide-12
SLIDE 12

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Homework Points

12

Pts Reason

2

  • turned in on time, AND
  • code runs, AND
  • solution is correct or nearly so, AND
  • demonstrates real effort

1

  • turned in on time, AND
  • partial solution, may not actually run, AND
  • demonstrates some effort (my discretion)
  • late, OR
  • is plagiarized (= Academic Miscondict), OR
  • does not demonstrate any real effort
slide-13
SLIDE 13

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Mailing List

compsci368-3-su12-dhh@lists.wisc.edu

  • Goes to @wisc.edu account
  • Check spam filters

13

slide-14
SLIDE 14

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Office Hours

Computer Sciences 4265 (Tim’s office) Days and times: Doodle poll today! Other times available by appointment (email)

14

slide-15
SLIDE 15

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

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

15

slide-16
SLIDE 16

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Some Perl Examples

16

slide-17
SLIDE 17

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

List and Count Directory Entries

17

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";

slide-18
SLIDE 18

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Average Numbers in Data File Columns

18

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";

slide-19
SLIDE 19

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Run a System Command As Another User

19 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);

slide-20
SLIDE 20

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

How to Run Perl

20

slide-21
SLIDE 21

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Running Perl

  • Unix/Linux

– perl filename – chmod 0755 filename ./filename

  • Mac OS X

– use Terminal, same as above

  • Windows

– download ActiveState Perl – not officially supported in the course

21

slide-22
SLIDE 22

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

Some Basic Syntax

22

slide-23
SLIDE 23

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Hello World

23

#!/usr/bin/perl use strict; use warnings; # Everyone's first Perl program print "Hello, world!\n";

slide-24
SLIDE 24

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Numbers & Math

24

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

  • literals: 42, 3.141, –6.5e9, 0377, 0xff
  • operators: + – * / ** % ( )
slide-25
SLIDE 25

Computer Sciences 368 Introduction to Perl

Cartwright 2012 Summer

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

25

slide-26
SLIDE 26

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

http://pages.cs.wisc.edu/~cs368-3/

26