12
IT441 NETWORK SERVICES ADMINISTRATION Prof. Alfred J Bird, Ph.D., NBCT http://www.cs.umb.edu/~abird [email protected] http://it441-s14-bird.wikispaces.umb.edu/ Office – McCormack 3rd floor 607 Office Hours – Tuesday and Thursday 4:00PM to 5:15PM

Prof. Alfred J Bird, Ph.D., NBCT abird [email protected] Office – McCormack 3rd floor 607

Embed Size (px)

Citation preview

Page 1: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

IT441

NETWORK SERVICES ADMINISTRATION

Prof. Alfred J Bird, Ph.D., NBCThttp://www.cs.umb.edu/~abird

[email protected]

http://it441-s14-bird.wikispaces.umb.edu/

Office – McCormack 3rd floor 607Office Hours – Tuesday and Thursday 4:00PM to

5:15PM

Page 2: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

Subroutines

What is a subroutine? How is it different from a function?

Why do we use subroutines?

Page 3: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

How do we make a subroutine?

Subroutines in Perl have three parts: The declaration sub The name of the subroutine

The name may contain a list of parameters Make the name mean something to you

A block of code enclosed in curly braces { actions }

The subroutine can contain any code that the main routine can contain. It can even call other subroutines.

Page 4: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

How do we invoke a subroutine?

The most common way is to just refer to it by its name followed by a set of parentheses ( )

exampleSubroutine() This can call a subroutine defined

anywhere in the file. If the subroutine is defined prior to its

invocation the parenthesis can be omitted.

exampleSubroutine

Page 5: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

Other ways to invoke a subroutine

We can also invoke the subroutine before is is defines if we let the code know it is a subroutine. We can do the by: By including the statement sub

exampleSubroutine; prior to invoking it Or calling it by &exampleSubroutine; You can think of the & as a type declaration

sort of like $, @ and % The first method is the more common

Page 6: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

Subroutine Argument List

Arguments (parameters) can be passed into the subroutine inside an array in parenthesis following the name.

Arguments are passed by reference, not by name or value

Arguments are passed in the array @_ You should not use the array @_ directly

but should assign it to another arraymy @args = @_ ;

Page 7: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

Return values

Subroutines return a value. It is the result of the last assignment completed.my $value = exampleSubroutine();

You can use a return statement in a subroutine As soon as the first return statement is

reached, control is returned to the calling program.

Page 8: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

Variable Scope

There are two main types of variables. Global or package variables:

Global variables are accessible anywhere in the program

Lexical or local variables Only accessible within the block of code where

they are defined Defined with a my statement

Why do we have two types of variables? All variables are global by default!

Page 9: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

The Overall World of PERL What is a namespace? What is a package?

Packages are perl files with .pm extn and are considered a separate namespace. So a package is nothing but group of related scalars,arrays,hashes and subroutines for a specific purpose. Once a package is included in a .pl file (using "use") and you want to call one of the subroutines of the package, you may have to use the scope resolution operator &package::subroutine1

Page 10: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

The Overall World of PERL

What is a module? Modules are packages which have the

capabilities of exporting selective subroutines/scalars/arrays/hashes of the package to the namespace of the main package itself. So for the interpreter these look as though the subroutines are part of the main package itself and so there is no need to use the scope resolution operator while calling them.

Page 11: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

CPAN

Why use PERL?\ What other languages could we use?

Ruby, Python, Scripting….. TIMTOWTDI!! Other people have already done it! http://www.perl.org http://www.cpan.org http://www.perlmonks.org

Page 12: Prof. Alfred J Bird, Ph.D., NBCT abird abird@cs.umb.edu  Office – McCormack 3rd floor 607

For next time

In the book “Beginning Perl” Read and understand pages 131-152 Read pages 215 – 229

In the book “Beginning Ubuntu Server”

Read pages 60 - 72