1 / 12

IT441 Network Services Administration

IT441 Network Services Administration. Prof. Alfred J Bird, Ph.D., NBCT http://www.cs.umb.edu/~abird abird@cs.umb.edu http:// it441-s14-bird.wikispaces.umb.edu / Office – McCormack 3rd floor 607 Office Hours – Tuesday and Thursday 4:00PM to 5:15PM. Subroutines. What is a subroutine?

totie
Download Presentation

IT441 Network Services Administration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IT441Network Services Administration Prof. Alfred J Bird, Ph.D., NBCT http://www.cs.umb.edu/~abird abird@cs.umb.edu http://it441-s14-bird.wikispaces.umb.edu/ Office – McCormack 3rd floor 607 Office Hours – Tuesday and Thursday 4:00PM to 5:15PM

  2. Subroutines • What is a subroutine? • How is it different from a function? • Why do we use subroutines?

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

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

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

  6. 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 array my @args = @_ ;

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

  8. 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!

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

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

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

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

More Related