Subroutines and Context

Argument passing, subroutine definition, return values—these are the features of function definitions in any language. But, because this is Perl, there are always wrinkles. You already saw one of those wrinkles with the fact that individual arrays and hashes lose their identities when they're passed into subroutines as arguments. Another wrinkle is the issue of context.

Given that subroutines can be called in either a scalar or list context, and that they can return either a scalar or a list, context is a relevant issue to subroutine development. Or at least it's a relevant issue to keep in mind as you use your subroutines: be careful not to call subroutines that return lists in a scalar context, unless you're aware of ...

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.