Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
A Guide to Unix Using Linux Fourth Edition
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Perl Basics Chapters 1-6 of “Learning Perl” By Randal Schwartz, Tom Christiansen & Larry Wall; ISBN , 302 pages. Second Edition, July 1997.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
W3101: Programming Languages (Perl) 1 Perl Regular Expressions Syntax for purpose of slides –Regular expression = /pattern/ –Broader syntax: if (/pattern/)
Programming Languages Tucker and Noonan
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Practical Extraction & Report Language PERL Joseph Beltran.
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
CPS 506 Comparative Programming Languages Imperative Programming Language Paradigm.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Introduction to Perl Yupu Liang cbio at MSKCC
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Time to talk about your class projects!. Shell Scripting Awk (lecture 2)
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
5 1 Data Files CGI/Perl Programming By Diane Zak.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Organization of Programming Languages Meeting 37 April 18, 2016.
Perl Programming Language Design and Implementation (4th Edition)
Perl Variables: Array Web Programming.
Instructor: Alexander Stoytchev
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this darn machine; I wish they would sell it; It won’t do what I want it to, but only what I tell it. Programmer’s lament (anonymous)

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 12.1 What Makes a Language Imperative? 12.2 Procedural Abstraction 12.3 Expressions and Assignment 12.4 Library Support for Data Structures 12.5 C 12.6 Ada 12.7 Perl

Copyright © 2006 The McGraw-Hill Companies, Inc. Perl is: widely used a scripting language (originally for Unix) dynamically typed encourages a variety of styles supports regular expression pattern matching

Copyright © 2006 The McGraw-Hill Companies, Inc. “Larry Wall... created Perl when he was trying to produce some reports from a Usenet-news-like hierarch of files for a bug reporting system, and awk ran out of steam. Larry, being the lazy programmer that he is, decided to over-kill the problem with a general purpose tool that he could use in at least one other place. The result was the first version of Perl.”

Copyright © 2006 The McGraw-Hill Companies, Inc. Scripting Languages “glue” take output from one application and reformat into desired input format for a different application. most time is spent in the underlying applications. also used for Web applications

Copyright © 2006 The McGraw-Hill Companies, Inc. General Characteristics dynamically typed default conversion from one type to another (vs. Python) result is distinct operators; ex:. for string concatenation types: numbers, strings, regular expressions dynamic arrays: indexed and associative

Copyright © 2006 The McGraw-Hill Companies, Inc. String vs. numeric comparisons: 10 < 2 # false - numeric 10 < "2" # false "10" lt "2" # true - string 10 lt "2" # true

Copyright © 2006 The McGraw-Hill Companies, Inc. Indexed = (2, 3, 5, 7); # size is 4... $a[7] = 17; # size is 8; # $a[4:6] are undef

Copyright © 2006 The McGraw-Hill Companies, Inc. Associative Arrays %d = (“bob” => “3465”, “allen” => “3131”, “rebecca” => “2912”); print $d{“bob”};# prints 3465

Copyright © 2006 The McGraw-Hill Companies, Inc. Many different ways of saying the same thing Much of the syntax is optional; ex: ( ) Perl 5 added support for classes and objects Great strengths: support for regular expressions Many irregularities in Perl

Copyright © 2006 The McGraw-Hill Companies, Inc. #! /usr/bin/perl die "Usage mygrep string \n" < 1; use strict; my $string = shift; my $ct = 0; while (<>) { $ct++; print "$ct:\t$_" if /$string/; } exit;

Copyright © 2006 The McGraw-Hill Companies, Inc. Scalar variables start with a $ Indexed arrays with Hash arrays with % Otherwise: bare word syntax error

Copyright © 2006 The McGraw-Hill Companies, Inc. use strict forces declaration of variables –local : dynamic scoping –my : static scoping –NB: only 1 $_

Copyright © 2006 The McGraw-Hill Companies, Inc. Strings Double quotes: special characters interpreted –ex: “$a \n” –forms: “ “, qq{ }, qq/ / Single quotes: special characters uninterpreted –forms: ‘ ‘, q{ }, q/ /

Copyright © 2006 The McGraw-Hill Companies, Inc. while (<>) {... } is same as: while ($_ = ) {... } where: <> is read a line returns undef at end of file; undef interpreted as false no subject: $_ if $_ =~ m/pattern/ # implied subject, operator

Copyright © 2006 The McGraw-Hill Companies, Inc. #! /usr/bin/perl if < 1) { die "Usage mygrep string \n" ; } use strict; my $string = my $ct = 0; my $line; while ($line = ) { $ct++; if ($line =~ m/$string/) { print STDOUT $ct, ":\t", $line; } exit;

Copyright © 2006 The McGraw-Hill Companies, Inc. Ex: Mailing Grades typical glue program student grades kept in a spreadsheet after each project/test, mail each student her grades include averages export data in comma-separated value format (CSV) CSV format varies by spreadsheet

Copyright © 2006 The McGraw-Hill Companies, Inc. Fig ::Proj1:Test1:::::Total:Average ::50:100::::::150: Average::88:91:::::135:90

Copyright © 2006 The McGraw-Hill Companies, Inc. Main program - part 1 retrieves class designation from command line opens CSV file for input or die is a common idiom declares some useful constants (some should be command line options)

Copyright © 2006 The McGraw-Hill Companies, Inc. #! /usr/bin/perl use strict; my $class = shift; my $suf = ".csv"; open(IN, "<$class$suf") || die "Cannot read: ". "$class$suf\n"; my $sep = ":"; my $tab = 8;

Copyright © 2006 The McGraw-Hill Companies, Inc. Main program - part 2 reads grade column names reads maximum points for each column adds 100% array

Copyright © 2006 The McGraw-Hill Companies, Inc. # read header lines: titles, max grades = &readSplit(); = &readSplit(); '100%');

Copyright © 2006 The McGraw-Hill Companies, Inc. Main program - part 3 loop reads 1 student per iteration (line) chomp() idiom; irregular tr deletes “, ' tokenizer for Perl last line pops averages from student array and splits values into columns (printed with each student)

Copyright © 2006 The McGraw-Hill Companies, Inc. # read students while ( ) { chomp; tr /"'//d; $_); } = split(/$sep/,

Copyright © 2006 The McGraw-Hill Companies, Inc. Main - part 4 for loop generates mail, 1 student per iteration student grades split into columns subroutine call; & optional mails averages to script invoker prints number of student s generated

Copyright © 2006 The McGraw-Hill Companies, Inc. # gen mail for each student my $ct = 0; foreach { = split(/$sep/); $ct += } $ave[1] = $ENV{"USER"}; print " s sent: $ct\n"; exit;

Copyright © 2006 The McGraw-Hill Companies, Inc. sub readSplit reads a line; note $_ is global chomp idiom tr deletes quotes splits line and returns array of columns

Copyright © 2006 The McGraw-Hill Companies, Inc. sub readSplit { $_ = ; chomp; tr /"'//d; = split(/$sep/); }

Copyright © 2006 The McGraw-Hill Companies, Inc. sub sendMail no formal parameters shift -- call by array reference -- call by reference return if student has no address open pseudo file or die –MAIL is a pipe to Berkeley mail command –s option is subject –$ is mail address

Copyright © 2006 The McGraw-Hill Companies, Inc. sub sendMail { my $name = shift; my $ = shift; return 0 unless $ ; open(MAIL, "| mail -s '$class Grades' $ ") || die "Cannot fork mail: $!\n"; print MAIL "GRADE\t\tYOUR\tMAX\tCLASS\n", "NAME\t\tSCORE\tSCORE\tAVE\n\n";

Copyright © 2006 The McGraw-Hill Companies, Inc. sub sendMail -- for loop for each column skip column if empty header -- not a grade otherwise print header if column value starts with a digit, round value to an integer; otherwise print value { } required; reason for backwards if/unless print maximum points print average (rounded)

Copyright © 2006 The McGraw-Hill Companies, Inc. my $ct = 1; foreach { $ct++; next unless $hdr[$ct]; print MAIL "$hdr[$ct]\t"; print MAIL "\t" if length($hdr[$ct]) < $tab; if (/^\d/) { print MAIL int($_ + 0.5); } else { print MAIL $_; }

Copyright © 2006 The McGraw-Hill Companies, Inc. print MAIL "\t$max[$ct]\t"; if ($ave[$ct] =~ /^\d/) { print MAIL int($ave[$ct] + 0.5); } else { print MAIL $ave[$ct];} print MAIL "\n"; } # foreach return 1; } # sub sendMail

Copyright © 2006 The McGraw-Hill Companies, Inc. $_ : implied object/subject $ARG : default input $. : input line number $/ : input record separator : default \n –undef $/; $_ = ; # reads entire file $, : output field separator in print $\ : output record separator $" : list separator : default space $[ : starting array index : default zero 10 pages of these; also names

Copyright © 2006 The McGraw-Hill Companies, Inc. Regular Expressions Operators m// -- match, m optional s/// -- substitute split -- array returning function

Copyright © 2006 The McGraw-Hill Companies, Inc. Modifiers i -- case insensitive m -- treat string as multiple lines s -- treat string as a single line x -- extend with whitespace and comments

Copyright © 2006 The McGraw-Hill Companies, Inc. Most characters match themselves Meta characters: \ | ( ) [ { ^ $ * + ?. ^ if first -- match from beginning of string $ if last -- match until end of string. -- single character wildcard * + ? -- Kleene star Questions –Empty string? –Entire string? –First x to end of string?

Copyright © 2006 The McGraw-Hill Companies, Inc. [ ] -- set –letters, digits, vowels [^ ] -- not in set | -- or ( ) -- grouping predefined sets (lc) and their inverses (uc) –\d : digits –\w : letters plus digits –\s : whitespace