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, ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.