Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen."— Presentation transcript:

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

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

3 Topics of Discussion n How Do Subroutines Function? n Modularize It, Package It, and Send It to the Library!

4 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

5 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

6 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

7 Passing Arguments n Call-by-Reference and the @_Array 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 10.10 and 10.11

8 Prototypes n 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

9 Return Values n The value returned is really the value of the last expression evaluated within the subroutine. Example 10.14

10 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 10.15 and 10.16 u Passing Filehandles by Reference Example 10.17 u Select Aliasing and the Backslash Operator Example 10.18

11 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

12 Autoloading n The Perl 5 AUTOLOAD function lets you check to see if a subroutine has been defined. Examples 10.23 and 10.24

13 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

14 The subs Function n The subs function allows you to pre- declare subroutine names. Example 10.26

15 Chapter 11: Modularize It, Package It, And Send It to the Library! n Packages and Modules n The Standard Perl Library

16 Packages and Modules n An Analogy n Definition n The Symbol Table

17 An Analogy n Packages n Symbols (names for variables and constants) n The Ideal: Keeping symbols in their own private packages.

18 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.

19 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.

20 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.

21 The examples n Example 11.1 n Example 11.2 n Example 11.3

22 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)

23 The Standard Perl Library n Using a Perl Module from the Standard Perl Library Examples: 11.9 and 11.10 n Using Perl to create your own module Example: 11.12 n Modules from CPAN Example: 11.13

24 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

25 Lab n Examples 10.1 – 10.26 (P 289 - 321) n Examples 11-1 – 11.13 (P 325 – 352) n Homework 6

26 Next Week n Reading assignment (Textbook chapter 12 and Chapter 16)


Download ppt "Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen."

Similar presentations


Ads by Google