<?xml version="1.0" encoding="windows-1252"?>
<node id="1014474" title="Re: foreach-loop-local var in sub" created="2013-01-21 10:55:37" updated="2013-01-21 10:55:37">
<type id="11">
note</type>
<author id="72516">
blue_cowdawg</author>
<data>
<field name="doctext">
&lt;ul&gt;&lt;ul&gt;&lt;i&gt;

&lt;/i&gt;&lt;/ul&gt;&lt;/ul&gt;
&lt;p&gt;
First off you've declared the sub as having no arguments. Secondly within the curly braces of the sub you have changed context and &lt;code&gt;$i&lt;/code&gt; is uninitialized within that context and therefor you are going to print zeros for each iteration. 
&lt;/p&gt;
&lt;p&gt;
Here is code that does what you think it should:
&lt;code&gt;
foreach (0, 1) {
    my $i = $_;
    sub my_print {
        my $i = shift;
        print $i;
    }

    my_print( $i);
}
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Now... you could have saved yourself a lot of pain if you had 
&lt;code&gt;
use strict;
use warnings;
&lt;/code&gt;
at the beginning of your code. Bailiff, lock him up.
&lt;/p&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-72516"&gt;
&lt;hr&gt;
&lt;font size="-2"&gt;

Peter L. Berghold  -- Unix Professional&lt;br&gt;
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg&lt;br&gt;
&lt;/font&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1014473</field>
<field name="parent_node">
1014473</field>
</data>
</node>
