|more useful options|
Passing subroutines as argumentsby mvaline (Friar)
|on May 08, 2003 at 19:38 UTC||Need Help??|
mvaline has asked for the
wisdom of the Perl Monks concerning the following question:
I'm working through the wonderful textbook Structure and Interpretation of Computer Programs and am trying to use some of what I'm learning in Perl instead of LISP.
One of the powerful things that LISP can do is accept procedures as arguments. This gives the programmer a lot of flexibility. The example given in SICP is that of the summation (think sigma notation). Using LISP, it is possible to model summation independently of the particular series being summed. Instead of writing individual summation procedures like this:
You can write something like this:
and use it like this:
Now I easily write summation procedures in Perl like this:
But what I want to do is something like this:
But I can't figure out how to use a subroutine once I pass it to another subroutine. I know I'm passing the address of a subroutine using the &subroutine construct, but how do I run it when the reference to it is being stored in a scalar variable (i.e. the $term and $next variables in the sum subroutine above) ?