Docsity

Perl Programming: Scripting Language and Data Types at UNC Chapel Hill, Study notes of Programming Languages

Partial preview of the text

Download Perl Programming: Scripting Language and Data Types at UNC Chapel Hill and more Study notes Programming Languages in PDF only on Docsity!

Lecture 5/6: Scripting and Perl

COMP 524 Programming Language Concepts Stephen Olivier January 29, 2009 and February 3, 2009

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 vs Weakly Typed Langauges

• 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 late 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

Perl Overview

• Perl is interpreted.

• Every statement ends in a semicolon

• Comments begin with “#” and extend one line

  • We’ll see how to do multi-line comments later

• What Perl doesn’t do well:

  • Complex algorithms and data structures.
  • Well defined and slowly changing functions.

10

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.

11

Scalars in Strings

• To use a scalar in a string simple insert it!

$A = print (“A’s 1; value is $A \n”);

13

Addition and Concatenation

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

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

$B = 2;$A = 1;

$C = $A + $B;

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

Built in Data type: Array

• Array variables begin with “@”

• Using “=(xxx,yyy,zzz,...)” we can define the content of the array

• Using $foo[xxx] we can access individual elements of the array @foo.

@A;

@A = (1, “two”, 3.13, true);

print ($A[1]); #Prints “two” 16

Built in Data type: Array

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

• There is a default array “@_”

@A = print $#A; (1, “two”, #Prints 4 3.13, true);

17

Built in Data Types: Hash

• Great for text processing

  • Building tables, lists, etc....

• Built-in function “keys” gets all subscripts.

%A foreach = (‘first’, 1, ‘junk’, ‘value’, 3.14, true); (keys (%A)) { #Loads values in t “$” }^ print^ “(^ $A{$}):$_ \n”; 19

Control Flow

20

if($b <$b = 3; 10){ } $a = 5;elseif ($b < 20){ } $a = 15;else { }^ $a = -3; print($a); $c = print($c >= 3; 10? 20 : 10). “\n”;