In addition to the large number of Perl functions already available, you can create your own functions or subroutines. Some languages distinguish between the terms function and subroutine. Perl doesn't. If you say "subroutine," everyone will know you are talking about a "function," and if you say "function," everyone will know you are talking about a "subroutine."[1] Technically, a function is a block of code that returns a value, whereas a subroutine is a block of code that performs some task, but doesn't return anything. Perl subroutines and functions can do both, so we'll use the two terms interchangeably in this text. For now, we'll use the term "subroutine" when referring to user-defined functions.
[1] You could even change the title of this chapter to, "How Do Functions Subroutine?" but some people might frown.
Subroutines are self-contained units of a program designed to accomplish a specified task, such as calculating a mortgage payment, retrieving data from a database, or checking for valid input. When a subroutine is called in a program, it is like taking a detour from the main part of the program. Perl starts executing the instructions in the subroutine and when finished, returns to the main program and picks up where it left off. Subroutines can be used over and over again and thus save you from repetitious programming. They are also used to break up a program into smaller modules to keep it better organized and easier to maintain.
The subroutine declaration consists of one or more statements enclosed in a block, independent of your program and not executed until it is called. It is often referred to as a "black box." Information goes into the black box as input (like the calculator or remote control when you push buttons), and the action or value returned from the box is its output (such as a calculation or a different channel). What goes on inside the box is transparent to the user. The programmer who writes the subroutine is the only one who cares about those details. When you use Perl's built-in functions, such as print() or rand(), you send a string of text or a number to the function, and it sends something back. You don't care how it does its job; you just expect it to work. If you send bad input, you get back bad output or maybe nothing; hence the expression "Garbage in, garbage out."
The scope of a subroutine is where it is visible in the program. Subroutines are global and can be placed anywhere in the script, even in another file. When coming from another file, they are loaded into the script with the do, require, or use keywords. All variables created within a subroutine or accessed by it are also global unless specifically made local with either the local or my operators.
The subroutine is called, or invoked, by prefixing the subroutine name with an ampersand (&), by prefixing the subroutine with the do function,[2] or by appending a set of empty parentheses to the subroutine name. If a forward reference is used, neither ampersands nor parentheses are needed to call the subroutine.
[2] The primary use of the do function was to include Perl subroutines from the Perl 4 library (e.g., do'pwd.pl').
If a nonexistent subroutine is called, the program quits with an error message: Undefined subroutine in "main::prog" .... If you want to check whether the subroutine has been defined, you can do so with the built-in defined function.
The return value of a subroutine is the value of the last expression evaluated (either a scalar or an array). The return function can be used explicitly to return a value or to exit from the subroutine early due to the result of testing some condition.
If the call to the subroutine is made part of an expression, the returned value can be assigned to a variable, thus emulating a function call.
FormatSubroutine declaration: sub subroutine_name; Subroutine definition: sub subroutine_name { Block } Subroutine call: do subroutine_name; &subroutine_name; subroutine_name(); subroutine_name; Subroutine call with parameters: &subroutine_name(parameter1, parameter2, ... ) subroutine_name(parameter1, parameter2, ... ) |
A declaration simply announces to the Perl compiler that a subroutine is going to be defined in the program and may take specified arguments. Declarations are global in scope; in other words, they are visible no matter where you put them in the program although it is customary to put declarations at the beginning or end of the program, or in another file. A subroutine definition is a block of statements that follows the subroutine name. A subroutine that has not been explicitly declared is declared at the same time it is defined.
Declaration: | sub name_of_subroutine; |
Definition: | sub name_of_subroutine { statement; statement; } |
A subroutine can be defined anywhere in your program or even in another file. The subroutine consists of the keyword sub followed by an opening curly brace, a set of statements, and ending in a closing curly brace.
The subroutine and its statements are not executed until called. You can call a subroutine by preceding its name with an ampersand or by attaching a set of empty parentheses after its name or by doing neither and calling it as a built-in function. If you call the subroutine with neither the ampersand nor parentheses, then you must declare it first.
If the subroutine name is followed by parentheses (null parameter list), it can also be called without the ampersand.
#!/usr/bin/perl 1 $name="Ellie"; 2 print "Hello $name.\n"; 3 bye(); # Without parens or an ampersand, bye would be a bareword # causing a warning message when -w is used. 4 sub bye{ 5 print "Bye $name.\n"; } |
A forward reference announces to the compiler that the subroutine has been defined somewhere in the program. The ampersand is not needed to call a subroutine if it has been forward referenced.
#!/usr/bin/perl 1 sub bye; # Forward reference $name="Ellie"; 2 print "Hello $name.\n"; 3 bye; # Call subroutine without the ampersand 4 sub bye{ 5 print "Bye $name\n"; } (Output) 2 Hello Ellie. 5 Bye Ellie |
Scope describes where a variable is visible in your program. Perl variables are global in scope. They are visible throughout the entire program, even in subroutines. If you declare a variable in a subroutine, it is visible to the entire program. If you change the value of an already existing variable from within a subroutine, it will be changed when you exit the subroutine. A local variable is private to the block, subroutine, or file where it is declared. You must use either the local or my built-in functions to create local variables, since, by default, Perl variables are global in scope. (See "Call-by-Value with local and my" on page 332.)
(The Script) # Script: perlsub_sub2 # Variables used in subroutines are global by default 1 sub bye { print "Bye $name\n"; $name="Tom";} # Subroutine definition 2 $name="Ellie"; 3 print "Hello to you and yours!\n"; 4 &bye; 5 print "Out of the subroutine. Hello $name.\n"; # $name is now Tom 6 &bye; (Output) 3 Hello to you and yours! 1 Bye Ellie 5 Out of the subroutine. Hello Tom. 1 Bye Tom Explanation
|
[a] We are assuming that the program was compiled into one package, main. For more on packages and scope, see Chapter 12, "Modularize It, Package It, and Send It to the Library!"