programming perl in unix course number : cit 370 week 6 prof. daniel chen
TRANSCRIPT
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)
Topics of Discussion How Do Subroutines Function? Modularize It, Package It, and
Send It to the Library!
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
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
Defining and Calling a Subroutine
A Null Parameter List
Example 10.2 Forward Reference
Example 10.3 Scope of Variable
Example 10.4
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
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
Return Values
The value returned is really the value of the last expression evaluated within the subroutine.
Example 10.14
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
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
Autoloading
The Perl 5 AUTOLOAD function lets you check to see if a subroutine has been defined.
Examples 10.23 and 10.24
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
The subs Function
The subs function allows you to pre-declare subroutine names.
Example 10.26
Chapter 11: Modularize It, Package It, And Send It to the Library!
Packages and Modules The Standard Perl Library
Packages and Modules
An Analogy Definition The Symbol Table
An Analogy
Packages Symbols (names for variables and
constants) The Ideal: Keeping symbols in their
own private packages.
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.
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.
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.
The examples Example 11.1 Example 11.2 Example 11.3
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)
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
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
Lab
Examples 10.1 – 10.26 (P 289 - 321)
Examples 11-1 – 11.13 (P 325 – 352)
Homework 6
Next Week
Reading assignment (Textbook chapter 12 and Chapter 16)