title: Unexpected behavior in nested recursive function
body: <p>I have some code that behaves rather strangely.</p>
  
  <p>I am inside a function, and I declare a nested one, which should check if something isn't okay. If it's not then it should sleep for five seconds and call itself again.</p>
  
  <pre><code>sub stop {
  
      sub wait_for_stop {
          my $vm_view = shift;
  
          if ( $vm_view-&gt;runtime-&gt;powerState-&gt;val ne "poweredOff" ) {
              debug("...");
              sleep(5);
              wait_for_stop();
          }
      }
  
      debug("Waiting for the VM to stop");
      wait_for_stop( @$vm_views[0] );
  }
  </code></pre>
  
  <p>So, in the call that results in the recursion inside the <code>if</code> condition, if I put the parameter (as the function definition expects it), like this:</p>
  
  <pre><code>wait_for_stop($vm_view);
  </code></pre>
  
  <p>I get an infinite loop.</p>
  
  <p>If I leave it without a parameter, as in the code example above, it works as expected.</p>
  
  <p>Shouldn't <code>$vm_view</code> in the subsequent calls be empty? Or the last used value (<code>$vm_view-&gt;runtime-&gt;powerState-&gt;val</code>)? Either case should result in unexpected behavior and error.</p>
  
  <p>But it works without any parameter. So why is that? Is there something I've missed from perldoc?</p>
  
  <p><em>EDIT1:</em> Actually, the value of <code>$vm_views</code> does get changed, so that's not the reason for the infinite loop.</p>
  
  <h3>General clarifications</h3>
  
  <p>I am using the VMware SDK. The <code>$vm_views</code> object contains the VM details. I am polling one of its methods to detect a change, in this particular case, I need to know when the machine is turned off. So, for lack of a better way, I make a call every 5 seconds until the value is satisfactory.</p>
  
  <p>My purpose is to stop the VM, make modifications that can only be made while it's off, and then launch it. </p>
  
  <h3>Actual question</h3>
  
  <p>When I don't pass a parameter, the block works as expected &ndash; it waits until the value is <code>poweredOff</code> (the VM is off), and continues, which doesn't make much sense, at least to me.</p>
  
  <p>In the case I put <code>$vm_view</code> as parameter, I get an infinite loop (the value will still get changed, since I'm calling a method).</p>
  
  <p>So I am wondering why the function works, when after the first call, <code>$vm_view</code> should be <code>undef</code>, and therefore, be stuck in an infinite loop? [undef ne "poweredOff" -> sleep -> recursion till death]</p>
  
  <p>And why, when I pass the expected value, it gets stuck?</p>
  
  <p>PS: To all those saying my recursion is weird and useless in this scenario &ndash; due to a myriad of reasons, I need to use such a format (it's better suited for my needs, since, after I get this bit working, I'll modify it to add various stuff and reuse it, and, for what I have in mind, a function seems to be the best option).</p>