<?xml version="1.0" encoding="windows-1252"?>
<node id="1004658" title="Re^3: Any difference between use and require regarding honoring prototype defined for sub?" created="2012-11-19 23:50:38" updated="2012-11-19 23:50:38">
<type id="11">
note</type>
<author id="968231">
Athanasius</author>
<data>
<field name="doctext">
&lt;p&gt;Hello [stewart_lee], and welcome to the Monastery!&lt;/p&gt;

&lt;blockquote&gt;
&lt;i&gt;I use 'use', then I should call the sub with ($$@@) instead of ($$\@\@)&lt;/i&gt;
&lt;/blockquote&gt;

&lt;p&gt;I think you may be confused about how prototypes work in Perl. Specifically, &lt;tt&gt;($$\@\@)&lt;/tt&gt; means the subroutine expects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a scalar&lt;/li&gt;
&lt;li&gt;another scalar&lt;/li&gt;
&lt;li&gt;an array (&lt;i&gt;not&lt;/i&gt; an array reference!)&lt;/li&gt;
&lt;li&gt;another array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The backslash in the prototype &amp;mdash; &lt;tt&gt;\@&lt;/tt&gt; &amp;mdash; means the array will be &lt;i&gt;converted into&lt;/i&gt; an array reference inside the sub. So, in your example,&lt;/p&gt;

&lt;code&gt;
Money::Finance::getMovingAve(5,$size,@values,@mv);

...

sub getMovingAve($$\@\@) {
             my ($count, $number, $values, $movingAve) =  @_;
...
&lt;/code&gt;

&lt;p&gt;the variables &lt;tt&gt;$values&lt;/tt&gt; and &lt;tt&gt;$movingAve&lt;/tt&gt; are assigned references to the arrays &lt;tt&gt;@values&lt;/tt&gt; and &lt;tt&gt;@mv&lt;/tt&gt;, respectively, &lt;i&gt;by virtue of the subroutine&amp;rsquo;s prototype&lt;/i&gt;.&lt;/p&gt;

&lt;p&gt;So, with [doc://use], the code is behaving as expected. But with [doc://require], the prototype is ignored because it is seen too late, so to make the sub work correctly you have to pass references explicitly.&lt;/p&gt;

&lt;p&gt;Perl prototypes are confusing, and should probably be avoided unless you have a good reason to use them. Make sure to read [id://861966].&lt;/p&gt;

&lt;p&gt;By the way, you would probably have received help sooner had you posted a trimmed-down (but still working) version of the code showing only the relevant parts. See [id://174051]&lt;/p&gt;

&lt;p&gt;And &lt;i&gt;do not&lt;/i&gt; comment out &lt;tt&gt;use strict;&lt;/tt&gt; &amp;mdash; it&amp;rsquo;s there to make your life easier!&lt;/p&gt;

&lt;p&gt;Hope that helps,&lt;/p&gt;

&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-968231"&gt;
&lt;p&gt;Athanasius&amp;emsp;&lt;font color=#008000&gt;&amp;lt;&amp;deg;(((&amp;gt;&amp;lt;&lt;/font&gt;&amp;emsp;&lt;i&gt;contra mundum&lt;/i&gt;&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1004597</field>
<field name="parent_node">
1004608</field>
</data>
</node>
