Summary
In this chapter we saw how to declare and call subroutines. Specifically, we looked at anonymous subroutines, subroutine references, and the use strict subs pragma. Next, we looked at predeclaring subroutines and learned how to override built-in functions. After this, we learned about the internal stack, which Perl uses to hold details of subroutines, and saw an example of a recursive subroutine.
We went on to examine how parameters are passed to subroutines, and in particular the passing of scalars, lists, and hashes, converting scalar subroutines into list processors, passing the special array @_ directly into subroutines and implementing named parameters. We also looked at Perl’s support for subroutine prototypes, which provide compile-time checking of the number and type of parameters passed to a subroutine. We saw how to define the number of parameters, how to pass variables by reference instead of by value, making parameters optional, and when necessary, disabling prototypes in subroutines calls. Following this, we looked at how to return values from subroutines. We also saw how to return the undefined value and determine and respond to the calling context. Finally, we covered attribute lists and looked at defining attributes on subroutines, accessing attributes, special attributes, and package attributes.
Keywords
Array Variable List Operator Code Reference Empty List List ContextPreview
Unable to display preview. Download preview PDF.