Summary

A subroutine is a way of collecting together commonly used code or of portioning a larger script into smaller bits. Today you learned all about defining, calling, and returning values from subroutines, including delving into some of the issues of local and global variables.

Subroutines are defined using the sub keyword, the name of a subroutine, and a block containing the definition of the subroutine itself. Inside that block, you can define my variables that have a scope local to that subroutine, use the special @_ variable to get to the arguments that were passed into the subroutine, and use the return function to return a value (or list of values) from that subroutine. If the context in which the subroutine was called is relevant, the wantarray function will tell you how your subroutine was called.

When you call subroutines, you use the name of the subroutine with an option & at the beginning, and with the arguments to the subroutine inside parentheses. Perl passes all the arguments to the subroutine as a flat array of all the values of its arguments, expanding any nested arrays and copying all the values into the local variable @_.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset