CSCI-GA.3033.003 Scripting Languages Anonymous Functions

CSCI-GA.3033.003 Scripting Languages Anonymous Functions

• CSCI-GA.3033-003 NYU • 6/14/2012 Context, Modules, Glue (Perl) Anonymous functions • Function definition can omit name sub [id] [proto] [attrs] [{…}] CSCI-GA.3033.003 Higher-order function: • Example script: has function param or #!/usr/bin/env perl returns function result Scripting Languages use warnings; use strict; sub callFn { my ($fn, $x) = @_; &$fn($x); } 6/14/2012 sub printIt { my ($it) = @_; print "printIt $it\n”; } #pass reference to named subroutine; prints "printIt Hello" callFn(\&printIt, "Hello"); Context and Modules (Perl) #pass reference to anonymous subroutine; prints "lambda Hi" Scripting as Glue callFn(sub { my ($it) = @_; print "lambda $it\n" }, "Hi"); • Useful for call-backs, e.g., sort/compare © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 1 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 2 Perl Outline Idiomatic Perl • Regular expressions (continued) • Interpreter specification: #! • Type conversions • Regular expressions extract data • Programming in the large – Captured groups: $1, $2, … • Hashes and arrays store data • Scripting as glue – Nested references build up data structures • Default operand: $_ • Parameter array: @_ • String interpolations report results © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 3 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 4 Perl Soap-box Example from 5/24/2012, Revisited Readable Perl #!/usr/bin/perl -w %cup2g = ( flour => 110, sugar => 225, butter => 225 ); %volume = ( cup => 1, tbsp => 16, tsp => 48, ml => 236 ); • Use pragmata: warnings, strict, %weight = ( lb => 1, oz => 16, g => 453 ); while (<>) { maybe also diagnostics, sigtrap my ($qty, $unit, $ing) = /([0-9.]+) (\w+) (\w+)/; if ($cup2g{$ing} && $volume{$unit}) { • Avoid default $_ $qty = 1.0 * $qty * $cup2g{$ing} / $volume{$unit}; $unit = 'g'; • Name your subroutine parameters } elsif ($volume{$unit}) { $qty = 1.0 * $qty * $volume{ml} / $volume{$unit}; • Use my, avoid local $unit = 'ml'; } elsif ($weight{$unit}) { • Use /x modifier on regular expressions $qty = 1.0 * $qty * $weight{g} / $weight{$unit}; $unit = 'g'; – Add whitespace, line breaks, comments } printf("%d $unit $ing\n", $qty + .5); • Use modules for large projects } © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 5 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 6 • © Martin Hirzel • 1 • CSCI-GA.3033-003 NYU • 6/14/2012 Context, Modules, Glue (Perl) Concepts Perl Inside a Regular Expression Chomsky Hierarchy Kind Construct Syntax Example Pattern Matches Type Languages Automata Rules Essentials Character Itself b b 0 Recursively Turing machine α→β Concatenation e e bc bc enumerable 1 2 Alternative (or) e1|e2 a|bc a, bc 1 Context Linear bounded B α γ→αδγ Repetition (≥0) e* a* , a, aa, aaa sensitive Turing machine Grouping (e) (a|b)c ac, bc 2 Context free Pushdown A→β Quantifier Optional e? (a|b)? , a, b automaton (BNF) Repetition (≥1) e+ a+ a, aa, aaa 3 Regular Finite state A→b, C→dE Repetition e{n} a{3} aaa machine (regexp) Char class Custom class […] [a-c] a, b, c Wildcard character , , • Formal regexp only has “essentials” . a 3 : Shortcut class \… \d 0,1,2,…,9 • Perl adds shortcuts and non-regular features Assertion Zero-width anchor $,^,\b,… \b (word boundary) © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 7 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 8 Perl Perl Non-Regular Perl Regex Features Non-Regular Perl Regex Examples • Non-regular = not essential feature, and can not be emulated by essential features Description Regex Matched – Backtracking engine, may take exponential time Square (\d+)x\1 123x123 • Backreferences in same pattern: \1, \2, … (repeated) • Zero-width assertions: Palindrome (\w+)\w(??{reverse $1}) redivider – Look-ahead positive (?=…), negative (?!…) (mirrored) – Look-behind positive (?<=…), negative (?<!…) Balanced (<+)x(??{'>' x length $1}) <<<x>>> • Match-time code execution: (?{…}) parentheses • Match-time interpolation: (??{…}) • Backtracking suppression: (?>…) © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 9 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 10 Concepts Outline Natural and Artificial Languages English Perl • Regular expressions (continued) Concept Examples Feature Examples • Type conversions Noun fish Variable $line Pronoun it, they Default variable $_, @_ • Programming in the large Inflection …s Sigil @… • Scripting as glue Singular apple Scalar $i Plural oranges Array @row Verb cook Function sort Irregular verb read Operator <…> Infinitive to be Function reference &cmp Context go fish Context if(@a)… © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 11 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 12 • © Martin Hirzel • 2 • CSCI-GA.3033-003 NYU • 6/14/2012 Context, Modules, Glue (Perl) Perl Perl Context Primitive Conversions • When you use a value in a different context Value Context than the value’s type, Perl converts it Boolean Number String Reference – E.g., using array as scalar -> int length 0 false "" undefined Number identity • Context provided by: !=0 true "value" – Operator: e.g., string . string "" false 0 symbolic – Function: e.g., list identity print String "0" false 0 reference – Assignment: e.g., @a = list Other true prefix – Statement: e.g., if (boolean) {…} Undefined false 0 "" undefined • Functions provide context for parameters, and can react to context for return value Reference true addr "typ(adr)" identity © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 13 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 14 Perl Concepts List Conversions Weak/Strong, Static/Dynamic Typing Value Context Strong typing Scalar List (explicit conversions) ML Scalar identity 1-element list Scheme Java List literal last element flattened sublists VBA Array length identity PHP empty "0" empty list Hash JavaScript non-empty "size/buckets" k , v , k , v ,… C 1 1 2 2 Perl true iff match all groups m// Weak typing end of file "" empty array (implicit conversions) assembler <…> not EOF line all lines Static typing Dynamic typing (compile-time checks) (runtime checks) © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 15 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 16 Perl Perl Functions and Context Prototype Example • Prototype provides context for parameters • Example from page 227 of “Programming Perl”, 3rd ed. Wall, Christiansen, Orwant. O’Reilly, 2000. – sub [id] [proto] [attrs] [{…}] • See also the Error module on CPAN. – No prototype: list operator Function definitions Example usage – proto = (protoChar*) sub try(&$) { try { scalar, list, hash, file handle – $ @ % * my ($try, $catch) = @_; die "phooey"; – & subroutine (in first slot: bare block, no comma) eval { &$try }; } #not end of the call! – ; separates mandatory from optional arguments if ($@) { catch { – \ adds backslashes on actuals local $_ = $@; /phooey/ and &$catch; print "unphooey\n"; checks context for return value • wantarray } }; – true = list, false = array, undefined = void context } sub catch (&) { $_[0] } © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 17 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 18 • © Martin Hirzel • 3 • CSCI-GA.3033-003 NYU • 6/14/2012 Context, Modules, Glue (Perl) Perl Outline Modules • Module = file p/q.pm that starts with • Regular expressions (continued) declaration package p::q • Type conversions – Group library functions for reuse • Programming in the large – E.g., Math::BigInt, arbitrary precision arithmetic • Module import: access with unqualified names • Scripting as glue – 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 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 19 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 20 Perl Concepts Module Example Pluggable Type Systems use strict; use warnings; # put this code in a package apple; # file apple.pm sub create { • User chooses between multiple alternative my ($weight, $color) = @_; return { WEIGHT => $weight, COLOR => $color }; optional type systems, e.g., in Perl: } no implicit declarations sub pluck { – use strict "vars"; my $ref_to_hash = $_[0]; – use strict "refs"; no string→ref conversion return $ref_to_hash->{COLOR} . " apple"; } – use strict "subs"; no bareword strings sub prepare { my ($ref_to_hash, $how) = @_; – use warnings; same as perl -w return $how . "d " . pluck($ref_to_hash); – perl -T taint-flow checking } 1 # return true = success • Motivation: trade-off between ease of writing, #!/usr/bin/perl use strict; use warnings; use apple; # note the "use apple" maintainability, robustness, and security our $fruit = apple::create(150, "red"); # from a different file print apple::prepare($fruit, "slice"), "\n"; # "sliced red apple" – Similar to “Option Explicit” in VBA © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 21 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 22 Perl Perl Structure of a Perl Application Using Objects Compilation unit Usually, file; also, -e, eval arg #!/usr/bin/perl Import class module Package Named global namespace use strict; use warnings; use Apple; may vary by block; usually: one per file our $a1 = new Apple(150, "green"); Constructor call forms our $a2 = Apple->new(150, "green"); (indirect obj vs. arrow) Module p/q.pm file with package p::q $a2->{COLOR} = "red"; Set hash entry Pragma Module that changes language print $a1->prepare("slice"), "\n"; Method call Class Package used to bless reference print $a2->prepare("squeeze"), "\n"; (arrow form) Subroutine Named subroutines don’t nest Statements In subroutine or directly in file © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 23 © Martin Hirzel CSCI-GA.3033.003 NYU 6/12/12 24 • © Martin Hirzel • 4 • CSCI-GA.3033-003 NYU • 6/14/2012 Context, Modules, Glue (Perl) Concepts Perl Classes and Objects Defining Classes use strict; use warnings; package Fruit; sub new { Fruit class = package used my ($cls, $weight) = @_; return bless({ WEIGHT => $weight }, $cls); WEIGHT to bless references instance name : blessing package } sub pluck { new() my $self = $_[0]; name $a1 : Apple return "fruit(" . $self->{WEIGHT} . "g)"; pluck() Apple } prepare() WEIGHT = 150 sub prepare { WEIGHT my ($self, $how) = @_; COLOR = “green” return $how .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    7 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us