Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Let a module call a sub of the caller

by sundialsvc4 (Abbot)
on Sep 06, 2012 at 22:44 UTC ( #992201=note: print w/ replies, xml ) Need Help??


in reply to Let a module call a sub of the caller

This is an excellent application for what is known in computer-science circles as a closure.   Which Perl supports very well.

You can, within a subroutine, define a variable to be a “code reference,” that is to say, to a particular sub that you define on-the-spot and which can refer to local variables or anything else that is “visible” at the particular place where you define that sub.  

Your caller should simply define one of those closures, and pass it in as a parameter to the routine, who simply calls it.   The closure will execute in the context of the code that created it ... that is to say, “your caller.”

“It’s slick to the point of being positively elegant,” and it works.   (May I kindly refer you to a plethora of existing docs here and elsewhere on the Internet for the gory procedural details.)


Comment on Re: Let a module call a sub of the caller

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-09-30 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (385 votes), past polls