CSCI-GA Scripting Languages

Similar documents
CSCI-GA Scripting Languages

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

COMS 3101 Programming Languages: Perl. Lecture 5

Hands-On Perl Scripting and CGI Programming

Classnote for COMS6100

PERL Scripting - Course Contents

COMS 3101 Programming Languages: Perl. Lecture 1

CS 5142 Scripting Languages

1. Introduction. 2. Scalar Data

CSCI-GA Scripting Languages

Perl. Many of these conflict with design principles of languages for teaching.

COMS 3101 Programming Languages: Perl. Lecture 6

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

CSCI 4152/6509 Natural Language Processing Lecture 6: Regular Expressions; Text Processing in Perl

Learning Perl Objects, References, and Modules

Perl. Perl. Perl. Which Perl

Shells & Shell Programming (Part B)

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Shells and Shell Programming

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

COS 320. Compiling Techniques

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

Perl Library Functions

Perl Tutorial. Diana Inkpen. School of Information Technology and Engineering University of Ottawa. CSI 5180, Fall 2004

Course Outline. Advanced Perl Programming. Course Description: Pre-requisites: Course Content: Tel: +44 (0) Fax: +44 (0)

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

CP150 - Advanced Perl Programming

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Shells and Shell Programming

What about Object-Oriented Languages?

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CS 105 Perl: Completing the Toolbox

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Assignment 1: Communicating with Programs

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

CS 5142 Scripting Languages

Pathologically Eclectic Rubbish Lister

Creating a Shell or Command Interperter Program CSCI411 Lab

Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CS 105 Perl: Perl subroutines and Disciplined Perl

Perl and Python ESA 2007/2008. Eelco Schatborn 27 September 2007

COMP 4/6262: Programming UNIX

$pat = '(?{ $foo = 1 })'; use re 'eval'; /foo${pat}bar/; # won't fail (when not under -T # switch)

5th State of the Onion. Larry Wall

COMS 6100 Class Notes 3

Shell Programming Systems Skills in C and Unix

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Modularity and Reusability I. Functions and code reuse

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Scripting Languages Course 1. Diana Trandabăț

Bash scripting. Can put together bash commands into one big file to run all at once! CIS c. 22/02/09 Slide 1

JavaScript CS 4640 Programming Languages for Web Applications

Perl Basics. Structure, Style, and Documentation

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Review of Fundamentals

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Fortunately, the layout is much more legible, more like BASIC's PRINT USING statement. Think of it as a poor man's nroff(1). nroff

User Interface Design. Model Hierarchy/Succession. User Interfaces aren t easy. Why User Interfaces are critical. Elements of good U/I design

A Crash Course in Perl5

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Intermediate Perl Table of Contents Intermediate Perl Foreword Preface Structure of This Book Conventions Used in This Book Using Code Examples

Lecture 02 The Shell and Shell Scripting

Chapter 3:: Names, Scopes, and Bindings

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

Subroutines. Subroutines. The Basics. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc.

Mills HPC Tutorial Series. Linux Basics II

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Lecture 7. Log into Linux New documents posted to course webpage

Part 1: Basic Commands/U3li3es

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

Naming in OOLs and Storage Layout Comp 412

Useful Unix Commands Cheat Sheet

Names, Scope, and Bindings

I/O and Shell Scripting

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Implementation of a simple shell, xssh

The Procedure Abstraction

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

Basic Linux (Bash) Commands

COMS 3101 Programming Languages: Perl. Lecture 2

JavaScript: Sort of a Big Deal,

CS 105 Perl: Modules and Objects

An Introduction to Software Architecture. David Garlan & Mary Shaw 94

Introduc)on to Unix and Perl programming

G Programming Languages - Fall 2012

COMP284 Scripting Languages Lecture 2: Perl (Part 1) Handouts

package YourModule; require = = qw(munge frobnicate); # symbols to export on request

Introduc)on to Unix and Perl programming

EECS2031 Winter Software Tools. Assignment 1 (15%): Shell Programming

CS Unix Tools & Scripting

Bash command shell language interpreter

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Linux shell scripting Getting started *

Transcription:

CSCI-GA.3033.003 Scripting Languages 9/20/2013 Context and Modules (Perl) Scripting as Glue 1

Outline Programming in the large Scripting as glue 2

Modules Module = file p/q.pm that starts with declaration package p::q Group library functions for reuse E.g., Math::BigInt, arbitrary precision arithmetic Module import: access with unqualified names Compile-time use p; runtime require p; Disable locally: no p; Pragma = pragmatic module Changes Perl behavior in fundamental way E.g., strict, forces variable declarations 3

use strict; use warnings; package apple; sub create { Module Example my ($weight, $color) = @_; return { WEIGHT => $weight, COLOR => $color ; sub pluck { my $ref_to_hash = $_[0]; return $ref_to_hash->{color. " apple"; # put this code in a # file apple.pm sub prepare { my ($ref_to_hash, $how) = @_; return $how. "d ". pluck($ref_to_hash); 1 # return true = success #!/usr/bin/perl use strict; use warnings; use apple; our $fruit = apple::create(150, "red"); print apple::prepare($fruit, "slice"), "\n"; # note the "use apple" # from a different file # "sliced red apple" 4

Concepts Pluggable Type Systems User chooses between multiple alternative optional type systems, e.g., in Perl: use strict "vars"; no implicit declarations use strict "refs"; no string ref conversion use strict "subs"; no bareword strings use warnings; same as perl -w perl -T taint-flow checking Motivation: trade-off between ease of writing, maintainability, robustness, and security Similar to Option Explicit in VBA 5

Structure of a Perl Application Compilation unit Usually, file; also, -e, eval arg Package Module Pragma Class Subroutine Statements Named global namespace may vary by block; usually: one per file p/q.pm file with package p::q Module that changes language Package used to bless reference Named subroutines don t nest In subroutine or directly in file 6

Using Objects #!/usr/bin/perl use strict; use warnings; use Apple; Import class module our $a1 = new Apple(150, "green"); our $a2 = Apple->new(150, "green"); $a2->{color = "red"; print $a1->prepare("slice"), "\n"; print $a2->prepare("squeeze"), "\n"; Constructor call forms (indirect obj vs. arrow) Set hash entry Method call (arrow form) 7

Concepts Classes and Objects class = package used to bless references instance name : blessing package name fields (hash keys) class method instance methods Apple WEIGHT COLOR new() pluck() prepare() $a1 : Apple WEIGHT = 150 COLOR = green $a2 : Apple WEIGHT = 150 COLOR = red objects (blessed references) fields (hash keys+values) 8

Defining Classes use strict; use warnings; package Fruit; sub new { my ($cls, $weight) = @_; return bless({ WEIGHT => $weight, $cls); sub pluck { my $self = $_[0]; return "fruit(". $self->{weight. "g)"; sub prepare { my ($self, $how) = @_; return $how. "d ". $self->pluck(); 1 Fruit WEIGHT new() pluck() prepare() Invocant (class name or object) is first argument bless(ref,pkg) associates object with class 9

Inheritance in Perl use strict; use warnings; package Fruit; sub new { my ($cls, $weight) = @_; return bless({ WEIGHT => $weight, $cls); sub pluck { my $self = $_[0]; return "fruit(". $self->{weight. "g)"; sub prepare { my ($self, $how) = @_; return $how. "d ". $self->pluck(); 1 use strict; use warnings; use Fruit; package Apple; our @ISA = ("Fruit"); sub new { my ($cls, $weight, $color) = @_; return bless({ WEIGHT => $weight, COLOR => $color, $cls); sub pluck { my $self = $_[0]; return $self->{color. " apple"; 1 Fruit WEIGHT new() pluck() prepare() Apple WEIGHT COLOR new() pluck() prepare() 10

Concepts Virtual Dispatch use strict; use warnings; package Fruit; sub new { my ($cls, $weight) = @_; return bless({ WEIGHT => $weight, $cls); sub pluck { my $self = $_[0]; return "fruit(". $self->{weight. "g)"; sub prepare { my ($self, $how) = @_; return $how. "d ". $self->pluck(); 1 use strict; use warnings; use Fruit; package Apple; our @ISA = ("Fruit"); sub new { my ($cls, $weight, $color) = @_; return bless({ WEIGHT => $weight, COLOR => $color, $cls); sub pluck { my $self = $_[0]; return $self->{color. " apple"; 1 Fruit WEIGHT new() pluck() prepare() Use class of reference to decide which Apple method to call WEIGHT COLOR new() pluck() prepare() 11

Outline Programming in the large Scripting as glue 12

Gluing Perl using Bash: Pipes Bash (Bourne again shell) is an interactive command prompt for Unix or Linux At the bash shell prompt, prog 1 prog 2 pipes STDOUT of prog 1 to STDIN for prog 2 Perl scripts work well in pipelines: read from STDIN, write to STDOUT Idiomatic Perl facilitates one-liners, e.g.: ls -l perl -e'while(<>){/(\d+)\s(\s+\s+){3(\s+)$/;print "$1 $3\n"' sort -rn head -5 Good for one-time use, bad for readability 13

Gluing Bash using Perl: ` ` Perl provides many common Unix commands as library functions chdir, chmod, kill, mkdir, rmdir, Like bash, Perl has file test operators -d, -e, -f, -r, -w, -x, Perl programs can embed shell commands with ` ` Returns output as string Error code goes in $? 14

Gluing Perl using Perl: eval Perl scripts can interpret embedded Perl code using eval #!/usr/bin/perl print "please enter an operator, e.g., '+':\n"; $op = <>; chomp $op; $command = 'print (42 '. $op. ' 7)'; print "running the command '$command':\n"; eval $command; print "\n"; Module Safe provides restricted eval (reval) as sandbox for untrusted code 15

Gluing Text using Perl: Heredocs Heredoc = multi-line text embedded in Perl #!/usr/bin/perl print "-Error-\n", <<POEM, "-David Dixon-"; Three things are certain: Death, taxes, and lost data. Guess which has occurred. POEM Variations: <<'id' suppress interpolation <<space lines up to blank line print(<<id 1, <<id 2 ); stacked heredocs 16

Common Perl Mistakes Detected Message Compiler Missing curleys Typical example if($x) print "x" User == instead of eq die if("1x"=="1y") Compiler No comma allowed after filehandle Interpreter Modification of a read-only value And many more print STDOUT, "hi" $x=10; $$x=20; 17

Soap-box Evaluating Perl Strengths String processing Regular expressions Interpolation One-liners Many operators Implicit operands Vibrant community Portability Weaknesses Write-only language Need references to nest arrays/hashes Grafted-on features Object-orientation Exception handling Language specified by implementation 18

Perl Culture Perl haikus and other poetry print STDOUT q, Just another Perl hacker, unless $spring Obfuscated Perl contest $_ = "wftedskaebjgdpjgidbsmnjgc"; tr/a-z/oh, turtleneck Phrase Jar!/; print; perl.org, cpan.org, perlmonks.org, pm.org 19

Suggestions for Perl Practice hw04 gets you points, but you may want to do more at your own leasure Sort CSV file by user-specified column Pretty-print C code as HTML Turn PID and PPID from ps into tree Use Perl to translate gnuplot into VBA code that draws a Powerpoint slide Write a Perl poem 20

Administrative Last Slide Pick up your graded homework and quiz Today s lecture Next lecture Context Modules Object-oriented Perl Scripting as glue AWK Prelim review 21

Scopes and Visibility caller of eval (if any) Name lookup: (package) { { (block) (block) (outer blocks) (compilation unit) Compilation unit = file, -e, or eval argument Package = global named namespace Can declare same package in multiple blocks, or even in multiple compilation units Only one package in effect at any point 22

Package Example #!/usr/bin/perl package p; our $x = 'px'; { package q; our ($x, $y) = ('qx', 'qy'); print "x ('qx' from block) $x \n"; print "p::x ('px' qualified access) $p::x \n"; print "q::x ('qx' qualified access) $q::x \n"; { package r::s; # name can include multiple qualifier levels our $x = 'rsx'; { package q; # can reopen in different block, even different file our $z = 'qz'; print "x ('rsx' block hides package) $x \n"; print "y ('qy' disallowed by 'use strict') $y \n"; print "z ('qz' from block) $z \n"; print "q::x ('qx' qualified access) $q::x \n"; print "r::s::x ('rsx' qualified access) $r::s::x\n"; print "x ('px' from compilation unit) $x \n"; print "q::z ('qz' qualified access) $q::z \n"; print "r::s::x ('rsx' qualified access) $r::s::x\n"; 23

Typeglobs and Symbol Tables Package p::q uses symbol table %p::q:: Symbol table entry $p::{'x' is typeglob *p::x Typeglob = symbol table entry E.g., *x holds $x, @x, %x, *x{scalar is the same as \$x, and similarly for ARRAY, HASH, CODE, IO *x{glob is the same as \*x *x{package returns package containing x *x{name returns name of typeglob Perl internally manipulates typeglobs to implement features, e.g., function use 24