<?xml version="1.0" encoding="windows-1252"?>
<node id="34361" title="Scoped Variables in a Recursive Function." created="2000-09-28 12:07:43" updated="2005-07-19 14:08:39">
<type id="115">
perlquestion</type>
<author id="25727">
kilinrax</author>
<data>
<field name="doctext">
While investigating &lt;tt&gt;Benchmark.pm&lt;/tt&gt; I decided to try benchmarking different sorting algorithms, and have run into trouble with my implementation of &lt;dfn&gt;Merge Sort&lt;/dfn&gt;;
&lt;code&gt;
sub Merge(@) {
  my @array = @_;
  MergeSort (0, $#array);
  return @array;

  sub MergeSort($$) {
    my $first = shift;
    my $last = shift;
    if ($last&gt;$first) {
      my $mid = int(($last+$first)/2);
      MergeSort($first, $mid);
      MergeSort($mid+1, $last);
      my @b;
      
      @b = ( @array[$first..$mid], @array[reverse($mid+1..$last)] );
      my ($i, $j, $k) = (0, $last-$first, $first);
      for (; $k&lt;=$last; $k++) {
        $array[$k] = ($b[$i]&lt;$b[$j]) ? $b[$i++] : $b[$j--];
      }
    }
  }
}
&lt;/code&gt;
The function works perfectly, but with warnings turned on, spits out the following:&lt;br&gt;
&lt;code&gt;
Variable "@array" will not stay shared at ./benchmark-sort.pl line [x].
&lt;/code&gt;
Where &lt;tt&gt;&amp;#91;x&amp;#93;&lt;/tt&gt; is the first line in &lt;tt&gt;MergeSort()&lt;/tt&gt; which manipulates &lt;tt&gt;@array&lt;/tt&gt;.&lt;br&gt;&lt;br&gt;
Presumably I'm doing something naive here to do with the scoping of &lt;tt&gt;@array&lt;/tt&gt;, which, in slightly different circumstances, could cause errors rather than just warnings.&lt;br&gt;&lt;br&gt;
So, what does the warning mean, and how should I declare &lt;tt&gt;@array&lt;/tt&gt; to avoid it? </field>
</data>
</node>
