Skip to main content

Subroutines

  • Chapter
Pro Perl
  • 1147 Accesses

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

Rights and permissions

Reprints and permissions

Copyright information

© 2005 Peter Wainwright

About this chapter

Cite this chapter

(2005). Subroutines. In: Pro Perl. Apress. https://doi.org/10.1007/978-1-4302-0014-7_7

Download citation

Publish with us

Policies and ethics