Copyright © tutorialspoint.com
The two terms function and subroutine are used interchangeably in Perl. A function is a named code block that is generally intended to process specified input values into an output value, although this is not always the case. For example, the print function takes variables and static text and prints the values on the screen.
Subroutines, like variables, can be declared (without defining what they do) or declared and defined. To simply declare a subroutine, you use one of the following forms:
sub NAME sub NAME PROTO sub NAME ATTRS sub NAME PROTO ATTRS
where NAME is the name of the subroutine you are creating, PROTO is the prototype for the arguments the subroutine should expect when called, and ATTRS is a list of attributes that the subroutine exhibits.
If you want to declare and define a function, then you need to include the BLOCK that defines its operation:
sub NAME BLOCK sub NAME PROTO BLOCK sub NAME ATTRS BLOCK sub NAME PROTO ATTRS BLOCK
You can also create anonymous subroutines - subroutines without a name by omitting the NAME component:
sub BLOCK sub PROTO BLOCK sub ATTRS BLOCK sub PROTO ATTRS BLOCK
To call a function you would use one of the following forms:
NAME NAME LIST NAME (LIST) &NAME
To give a quick example of a simple subroutine:
The first argument you pass to the subroutine is available within the function as $_, the second argument is $_, and so on. For example, this simple function adds two numbers and prints the result:
To call the subroutine and get a result:
The preceding subroutine is fairly simple, but what if you wanted to have named arguments? The simple answer is to assign the values of @_ to a list of variables:
The shift function is one of the .stack. operands supported by Perl. The shift function returns (and removes) the first element of an array. For example:
The effect is exactly the same as we have shown earlier but we have just obtained the arguments in a different way.
Return Values from a Function
The return value of any block, including those used in subroutines, is taken as the value of the last evaluated expression. For exampl,e the return value here is the result of the calculation.:
You can also explicitly return a value using the return keyword:
When called, return immediately terminates the current subroutine and returns the value to the caller. If you don't specify a value then the return value is undef.
A Function Call Context
The context of a subroutine or statement is defined as the type of return value that is expected. This allows you to use a single function that returns different values based on what the user is expecting to receive. For example, the following two calls to the getpwent function return a list or a scalar, according to what was used in the assignation:
In the first case, the user expects a scalar value to be returned by the function, because that is what the return value is being assigned to. In the second case, the user expects an array as the return value, again because a list of scalars has been specified for the information to be inserted into.
Here's another example, again from the built-in Perl functions, that shows the flexibility:
In this example, the value of $timestr is now a string made up of the current date and time, for example, Thu Nov 30 15:21:33 2000. Conversely:
Now the individual variables contain the corresponding values returned by localtime.
WARNING: Lvalue subroutines are still experimental and the implementation may change in future versions of Perl.
It is possible to return a modifiable value from a subroutine. To do this, you have to declare the subroutine to return an lvalue. See the following example
Now see the magic
Passing Lists to Subroutines
Because the @_ variable is an array, it can be used to supply lists to a subroutine. However, because of the way in which Perl accepts and parses lists and arrays, it can be difficult to extract the individual elements from @_. All the followings are valid
Finally when we receive thes values in@_ variable then we can not recognize if we had passed one array or two value arraysbecause finally it is getting merged into one.o
If you want to work with and identify the individual lists passed to Perl, then you need to use references:
The leading \ character tells Perl to supply a reference, or pointer, to the array. A reference is actually just a scalar, so we can identify each list by assigning the reference to each array within our subroutine. Now you can write your subroutineas follows:
Passing Hashes to Subroutines
When you supply a hash to a subroutine or operator that accepts a list, the hash is automatically translated into a list of key/value pairs. For example:
This will output .nameTomage19.. However, the same process works in reverse, so we can extract a list and convert it to a hash:
In this case, we output the key/value pairs of the hash properly, displaying each pair on its own line. As with arrays, care needs to be taken if you expect to pick out a single hash from a list of arguments. The following will work because we extract the hash last:
while this one won.t because we try to extract the hash first (there will be an extra element, and Perl won.t know how to assign this to the hash):
If you want to work with multiple hashes, then use references. For example, the following subroutine returns the key intersection of two hashes:
To use the subroutine:
Copyright © tutorialspoint.com