Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen
Introduction n Review and Overviews n Chapters 10 and 11 n Summary n Lab n Next Week (Week 7)
Topics of Discussion n How Do Subroutines Function? n Modularize It, Package It, and Send It to the Library!
Chapter 10: How Do Subroutines function? n Defining and Calling a Subroutine n Passing Arguments n Prototypes n Return Values n Call-by-Reference – Aliases and Typeglobs n Passing by Pointer n Autoloading n BEGIN and END Subroutines (Start and Finish) n The subs Function
Defining and Calling a Subroutine n Subroutine Declaration sub name_of_subroutine; n Subroutine Definition Sub name_of_subroutine { statement; statement;} n Subroutine Call n Subroutine Call with Parameters Example 10.1
Defining and Calling a Subroutine n A Null Parameter List Example 10.2 n Forward Reference Example 10.3 n Scope of Variable Example 10.4
Passing Arguments n Call-by-Reference and Example 10.5 Example 10.6 n Call-by-Value with local and my u The local Function Example 10.7 u The my function Examples10.8 and 10.9 u Using the strict Progma (my and our) Examples and 10.11
Prototypes n A prototype tells the compiler how many and what types of arguments the subroutine should get when it is called. Examples and 12.13
Return Values n The value returned is really the value of the last expression evaluated within the subroutine. Example 10.14
Call-by-Reference – Aliases and Typeglobs n Definition u A typeglob is an alias for a variable. n Passing by Reference with Aliases u Making Aliases private – local versus my Examples and u Passing Filehandles by Reference Example u Select Aliasing and the Backslash Operator Example 10.18
Passing by Pointer n Definition u A hard reference, commonly called a pointer, is a scalar variable that contains the address of another variable. n De-referencing the pointer u Table 10.1 Examples 10.19, 10.20, 10.21, and 10.22
Autoloading n The Perl 5 AUTOLOAD function lets you check to see if a subroutine has been defined. Examples and 10.24
BEGIN and END Subroutines (Start and Finish) n The BEGIN and END subroutines may remind UNIX programmers of the special BEGIN and END patterns used in the awk programming language n The BEGIN has been liked to a constructor, and END a destructor. Example 10.25
The subs Function n 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! n Packages and Modules n The Standard Perl Library
Packages and Modules n An Analogy n Definition n The Symbol Table
An Analogy n Packages n Symbols (names for variables and constants) n The Ideal: Keeping symbols in their own private packages.
Definition n Encapsulation(Class) n Package(A Separate Namespace) A Separate name space means that Perl has A separate symbol table for all the variables in a named package. n All variables are global within the package. n The package mechanism allows you to switch namespaces, so that variables and subroutines in the package are private. n 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 n The extension of packages n 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. n The use function takes the module name as its argument and loads the module into your script.
The Symbol Table n Figure 11.2 – The package provides privacy n Each package has its own symbol table. Any time you use the package declaration, you switch to the symbol table for that package. n 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. n The variable assigned using the my function are not accessible outside their own packages.
The examples n Example 11.1 n Example 11.2 n Example 11.3
The Standard Perl Library n Packages and.pl Files u The require Function u Including standard Library Routines Examples: 11.6 and 11.7 u Using Perl to include your own library Example: 11.8 n Modules and.pm Files u The use Function u The Exporter Module F Table 11.1 (Exporting Symbols)
The Standard Perl Library n Using a Perl Module from the Standard Perl Library Examples: 11.9 and n Using Perl to create your own module Example: n Modules from CPAN Example: 11.13
Summary n Defining and Calling a Subroutine n Passing Arguments n Prototypes n Return Values n Call-by-Reference – Aliases and Typeglobs n Passing by Pointer n Autoloading n BEGIN and END Subroutines (Start and Finish) n The subs Function n Modules & Packages
Lab n Examples 10.1 – (P ) n Examples 11-1 – (P 325 – 352) n Homework 6
Next Week n Reading assignment (Textbook chapter 12 and Chapter 16)