<?xml version="1.0" encoding="windows-1252"?>
<node id="992201" title="Re: Let a module call a sub of the caller" created="2012-09-06 18:44:12" updated="2012-09-06 18:44:12">
<type id="11">
note</type>
<author id="647953">
sundialsvc4</author>
<data>
<field name="doctext">
&lt;p&gt;
This is an excellent application for what is known in computer-science circles as a &lt;em&gt;closure.&lt;/em&gt; &amp;nbsp; Which Perl supports very well.
&lt;/p&gt;&lt;p&gt;
You can, within a subroutine, define a variable to be a &amp;ldquo;code reference,&amp;rdquo; that is to say, to a particular &lt;tt&gt;sub&lt;/tt&gt; that you define on-the-spot and which can refer to local variables or anything else that is &amp;ldquo;visible&amp;rdquo; at the particular place where you define that &lt;tt&gt;sub&lt;/tt&gt;. &amp;nbsp; 
&lt;/p&gt;&lt;p&gt;
Your caller should simply define one of those closures, and pass it in as a parameter to the routine, who simply calls it. &amp;nbsp; The closure will execute in the context of the code that &lt;em&gt;created&lt;/em&gt; it ... that is to say, &amp;ldquo;your caller.&amp;rdquo;
&lt;/p&gt;&lt;p&gt;
&amp;ldquo;It&amp;rsquo;s slick to the point of being positively &lt;em&gt;elegant,&amp;rdquo;&lt;/em&gt; and it &lt;u&gt;works&lt;/u&gt;. &amp;nbsp; (May I kindly refer you to a plethora of existing docs here and elsewhere on the Internet for the gory procedural details.)
&lt;/p&gt;</field>
<field name="root_node">
992164</field>
<field name="parent_node">
992164</field>
</data>
</node>
