INSIDE PERL MOHAMMAD T. IRFAN 11/6/13 Origin Scripting language - - PDF document

inside perl
SMART_READER_LITE
LIVE PREVIEW

INSIDE PERL MOHAMMAD T. IRFAN 11/6/13 Origin Scripting language - - PDF document

11/6/2013 CSCI-2325: INSIDE PERL MOHAMMAD T. IRFAN 11/6/13 Origin Scripting language based on sh and awk Larry Wall: People often argue over the difference between a scripting language and a programming language. If you ask me,


slide-1
SLIDE 1

11/6/2013 1

CSCI-2325: INSIDE PERL

MOHAMMAD T. IRFAN 11/6/13

Origin

  • Scripting language based on sh and awk
  • Larry Wall: “People often argue over the difference between a scripting

language and a programming language. If you ask me, I’ll tell you that a script is what you hand the actors, and a program is what you hand the audience.”

  • It is very much an imperative programming language
  • Always compiled before execution
slide-2
SLIDE 2

11/6/2013 2

Variables

  • Implicitly declared
  • 3 namespaces for variables (with special start symbols for name)
  • Scalar: $
  • Array: @
  • Hash: %
  • Larry Wall’s explanation: It’s similar to natural languages
  • Implicit variables
  • Example: @_

Scalar Variables

  • May contain both numbers and strings
  • Depending on the context numbers are converted to strings and vice

versa

  • Allows changing the value of a scalar variable to any type– number of

string

slide-3
SLIDE 3

11/6/2013 3

Arrays

  • Arrays are dynamic
  • Can have “empty” cells, which are denoted as “undefined”
  • Array indexing cannot be checked

Scoping

  • Dynamic scoping: References to names are resolved dynamically
  • Allows my keyword to avoid unexpected effects
  • local keyword can be used for dynamic scoping
slide-4
SLIDE 4

11/6/2013 4

Example: Dynamic Scoping

$x = 5; $y = 10; myFunction(); # call subroutine– no argument print "x = $x, y = $y\n"; sub myFunction { local($x, $y); #make it my ($x,$y) to swap global var $x = 100, $y = 200; swap(); # no argument print "After swap: x = $x, y = $y\n"; } sub swap { $t = $x; #Is this the x in myFunciton or the global x? $x = $y; $y = $t; }

Type System

  • Review [Caution: there are different definitions of these terms]
  • A language is statically typed, if the types of all variables are fixed when

they are declared at compile time

  • A language is dynamically typed if the type of a variable can vary at run

time depending on the value assigned

  • A language is strongly typed if its type system can detect all type errors

either at compile time or at run time

  • “Weakly typed” is not well-defined – may not be related to strongly typed
  • Examples
  • C is statically typed, but not strongly typed (hint: pointer, array index)
  • Java is both statically and strongly typed
  • Perl is both dynamically and strongly typed
slide-5
SLIDE 5

11/6/2013 5

Semantics

  • Expression, assignment – similar to C
  • Copy semantics
  • Iterative statements
  • A variety of for, while, and do-while statements
  • Larry Wall’s philosophy: Often, there are many ways of expressing the same

idea using natural languages

  • Conditional statements
  • if-elsif-else statements, other types of if and unless statements
  • Subroutine
  • Parameters are passed implicitly: array @_
  • Can return multiple items using an array
  • Always returns the last computed value, even if there is no return statement
  • Object-oriented features
  • package
  • Exception handling

Applications

  • System administration
  • Text processing
  • Computational biology and AI
slide-6
SLIDE 6

11/6/2013 6

BUILDING A LEXICAL ANALYZER FOR CLITE

Input File

void main() { int i; double jVar2; jVar2 = 10 + i + 50.99; }

slide-7
SLIDE 7

11/6/2013 7

Desired Output

Type void main main ( ( ) ) { { Type int Identifier i ; ; Type double Identifier jVar2 ; ; Identifier jVar2 = = IntegerLiteral 10 + + Identifier i + + DoubleLiteral 50.99 ; ; } }

How to run perl with arguments?

  • perl lexer.pl source.clite
  • You may enter it in Padre: Run  Run Command
slide-8
SLIDE 8

11/6/2013 8

lexer.pl: A first attempt

Open input and output files Read each line from the file Process that line

slide-9
SLIDE 9

11/6/2013 9

  • Subroutine to print a token-lexeme pair to the output file
  • Used by the match() subroutine
slide-10
SLIDE 10

11/6/2013 10

slide-11
SLIDE 11

11/6/2013 11

A MORE ELEGANT WAY

slide-12
SLIDE 12

11/6/2013 12