Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992201]
NodeReaper serves mincemeat tarts with the cider

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2018-06-21 13:12 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.