Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
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 musing on the Monastery: (12)
As of 2014-12-22 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls