Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 4. Subroutines > Item 50. Create new subroutines with subroutines - Pg. 176

176 Chapter 4 Subroutines Things to remember Use lexical variables to make data private to subroutines. In Perl 5.10 or later, use state variables for private data. Make generator ("factory") subroutines that create new subroutines for you. Item 50. Create new subroutines with subroutines. If you are often calling the same subroutine with some of the same argu- ments, you can create a new subroutine that remembers some of the arguments for you. This is known as currying a subroutine. Suppose that you have a subroutine that filters a list for elements match- ing a pattern, and then sorts the results: sub my_sorted_grep { my ( $pattern, $array_ref ) = @_; my @results = sort grep /$pattern/o, @$array_ref;