Docsity

Lecture 5: Scripting and Perl - UNC Chapel Hill - Prof. Aaron Block, Study notes of Programming Languages

Partial preview of the text

Download Lecture 5: Scripting and Perl - UNC Chapel Hill - Prof. Aaron Block and more Study notes Programming Languages in PDF only on Docsity!

Lecture 5: Scripting and Perl

COMP 514 Programming Language Concepts Aaron Block January 30, 2007

Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Goal of Lecture

• Discuss background on Scripting languages and Perl.

2

System Programming Languages

• System languages (e.g., Pascal, C++, Java) replaced assembly languages.

  • Two main advantages:
    • Hide unnecessary details (high level of abstraction)
    • Strongly Typed.

4

Strongly Typed

• Under Assembly, any register can take any type of value (e.g., integer, string).

• Under take values of a particular type. Strongly Typed languages , a variable can only

  • For example, “int a” can only have values of type “integer”

5

Typing and “Degree of Abstraction”

Assembly (^) C^ C++^ Java

Tcl/Perl

Visual Basic Scripting

System Prog.

None Strong

Degree of Typing

Instructions/Statement^ (Level of Abstraction)

7

Perl (Practical Extraction and Report Language)

• Larry Wall Created Perl in let 80s

  • Originally designed to be more powerful than Unix scripting.
  • Wanted “naturalness” ... shortcuts, choices, defaults, flexibility.

• Perl is dense and Rich

  • “Swiss-army chainsaw”
  • “Duct tape for Web”
  • “There is more than one way to do it!”
  • Often experienced Perl programmers will need a manual when reading other people’s code. 8
  • The University of North Carolina at Chapel HillFrom www.xkcd.com

What Perl Doesn’t Do well

• Complex algorithms and data structures.

• Large datasets.

• Well defined and slowly changing functions.

11

Built-in Data types

• No type Declarations

• Perl has three types:

  • Scalar
  • Array
  • Hash (Associative Array)

• Integers, float, boolean, etc... are all of type Scalar.

13

Built-in Data Types: Scalar

• Scalars begin with “$”

• Can take on any integer, real, boolean, and string value

$A = 1; $B = “Hello”; $C $D = true = 3.14;

14

Addition and Concatenation

• To add two scalars together, we use “+”

• To concatenate two strings together, we use “.”

$B$A = 1; = 2;

$C = $A + $B;

$B$A = “hi”; = “bye”; $C = $A. $B; 16

Context

• When a scalar is used, the appropriate contex t: value is converted to the

$B$A = “hi”; = 3; $C = $A. $B; #C = “hi3”

$B$A = “4”; = 3; $C = $A. $B; #C = “43”

$B = 3;$A^ = “hi”; $C = $A + $B; #C = “3” $B = 3;$A^ = “4”; $C = $A + $B; #C = “7” 17

Built in Data type: Array

• Using “$#foo” we can get the max index of the array “@foo”

@A print = (1, “two”, 3.13, true); $#A; #Print’s four

19

Built in Data Types: Hash

• Hashes are like arrays, except that they are any scalar type , not just integer. indexed by

• Hash variables begin with “%”

• Can be defined as via “( ‘index-1’, value-1, ‘index-2’, value-2,...)

• Subscripts are accessed by “{}” and can be any scalar

%A

%A = (‘first’, 1, ‘junk’, ‘value’, 3.14, true); print $A(3.14); #Prints “true” 20