programming perl in unix course number : cit 370 week 6 prof. daniel chen

26
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Upload: penelope-boyd

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Programming Perl in UNIX

Course Number : CIT 370

Week 6

Prof. Daniel Chen

Page 2: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Introduction

Review and Overviews Chapters 10 and 11 Summary Lab Next Week (Week 7)

Page 3: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Topics of Discussion How Do Subroutines Function? Modularize It, Package It, and

Send It to the Library!

Page 4: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Chapter 10: How Do Subroutines function?

Defining and Calling a Subroutine Passing Arguments Prototypes Return Values Call-by-Reference – Aliases and Typeglobs Passing by Pointer Autoloading BEGIN and END Subroutines (Start and Finish) The subs Function

Page 5: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Defining and Calling a Subroutine

Subroutine Declaration

sub name_of_subroutine; Subroutine Definition

Sub name_of_subroutine

{ statement; statement;} Subroutine Call Subroutine Call with Parameters

Example 10.1

Page 6: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Defining and Calling a Subroutine

A Null Parameter List

Example 10.2 Forward Reference

Example 10.3 Scope of Variable

Example 10.4

Page 7: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Passing Arguments

Call-by-Reference and the @_Array

Example 10.5

Example 10.6 Call-by-Value with local and my

The local Function

Example 10.7 The my function

Examples10.8 and 10.9 Using the strict Progma (my and our)

Examples 10.10 and 10.11

Page 8: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Prototypes

A prototype tells the compiler how many and what types of arguments the subroutine should get when it is called.

Examples 10.12 and 12.13

Page 9: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Return Values

The value returned is really the value of the last expression evaluated within the subroutine.

Example 10.14

Page 10: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Call-by-Reference – Aliases and Typeglobs Definition

A typeglob is an alias for a variable. Passing by Reference with Aliases

Making Aliases private – local versus my

Examples 10.15 and 10.16 Passing Filehandles by Reference

Example 10.17 Select Aliasing and the Backslash Operator

Example 10.18

Page 11: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Passing by Pointer

Definition A hard reference, commonly called a

pointer, is a scalar variable that contains the address of another variable.

De-referencing the pointer Table 10.1

Examples 10.19, 10.20, 10.21, and 10.22

Page 12: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Autoloading

The Perl 5 AUTOLOAD function lets you check to see if a subroutine has been defined.

Examples 10.23 and 10.24

Page 13: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

BEGIN and END Subroutines (Start and Finish)

The BEGIN and END subroutines may remind UNIX programmers of the special BEGIN and END patterns used in the awk programming language

The BEGIN has been liked to a constructor, and END a destructor.

Example 10.25

Page 14: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

The subs Function

The subs function allows you to pre-declare subroutine names.

Example 10.26

Page 15: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Chapter 11: Modularize It, Package It, And Send It to the Library!

Packages and Modules The Standard Perl Library

Page 16: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Packages and Modules

An Analogy Definition The Symbol Table

Page 17: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

An Analogy

Packages Symbols (names for variables and

constants) The Ideal: Keeping symbols in their

own private packages.

Page 18: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Definition Encapsulation(Class) Package(A Separate Namespace)

A Separate name space means that Perl has A separate symbol table for all the variables in a named package.

All variables are global within the package. The package mechanism allows you to

switch namespaces, so that variables and subroutines in the package are private.

The scope of the package is from the declaration of the package to the end of the inner most enclosing block, or until another package is declared.

Page 19: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Modules The extension of packages A Module (.pm)

A package that is usually defined in a library. Modules can export symbols to another packages and to work with classes and methods.

The use function takes the module name as its argument and loads the module into your script.

Page 20: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

The Symbol Table Figure 11.2 – The package provides privacy Each package has its own symbol table. Any

time you use the package declaration, you switch to the symbol table for that package.

A variable assigned using the local function can be accessed in another package by using a scope resolution symbol (::) to qualify it by package name.

The variable assigned using the my function are not accessible outside their own packages.

Page 21: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

The examples Example 11.1 Example 11.2 Example 11.3

Page 22: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

The Standard Perl Library Packages and .pl Files

The require Function Including standard Library Routines

Examples: 11.6 and 11.7 Using Perl to include your own library

Example: 11.8 Modules and .pm Files

The use Function The Exporter Module

Table 11.1 (Exporting Symbols)

Page 23: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

The Standard Perl Library Using a Perl Module from the Standard

Perl Library

Examples: 11.9 and 11.10 Using Perl to create your own module

Example: 11.12 Modules from CPAN

Example: 11.13

Page 24: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Summary Defining and Calling a Subroutine Passing Arguments Prototypes Return Values Call-by-Reference – Aliases and

Typeglobs Passing by Pointer Autoloading BEGIN and END Subroutines (Start and Finish) The subs Function Modules & Packages

Page 25: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Lab

Examples 10.1 – 10.26 (P 289 - 321)

Examples 11-1 – 11.13 (P 325 – 352)

Homework 6

Page 26: Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen

Next Week

Reading assignment (Textbook chapter 12 and Chapter 16)