sibyurik:
To illustrate what the Anonymous Monk above posted, here's a pair of functions, one of which is using a global (funky), and the other a local (monkey):
use strict;
use warnings;
my $str1='';
sub funky {
my $count = shift;
return if $count > 5;
$str1 = 4*$count;
my $indent = "funky" . " "x$count;
print $indent,"before recurse: count=$count, str1=$str1\n";
funky($count+1);
print $indent,"after recurse: count=$count, str1=$str1\n";
}
sub monkey {
my $count = shift;
return if $count > 5;
my $str = 4*$count;
my $indent = "monkey" . " "x$count;
print $indent,"before recurse: count=$count, str=$str\n";
monkey($count+1);
print $indent,"after recurse: count=$count, str=$str\n";
}
funky(0);
monkey(0);
As you can see in the results below, the global variable $str1 got whacked, so has the same value after recursion for each level. The local value, however, retains its value, so can continue. The same thing is happening with your directory handle from readdir: When you recurse, you've clobbered the value, so you can't continue from that point. In fact, when the lowest level if your function is done, the directory handle is reporting "Nope, no more files" for each level on return.
$ perl recurse.pl
funkybefore recurse: count=0, str1=0
funky before recurse: count=1, str1=4
funky before recurse: count=2, str1=8
funky before recurse: count=3, str1=12
funky before recurse: count=4, str1=16
funky before recurse: count=5, str1=20
funky after recurse: count=5, str1=20
funky after recurse: count=4, str1=20
funky after recurse: count=3, str1=20
funky after recurse: count=2, str1=20
funky after recurse: count=1, str1=20
funkyafter recurse: count=0, str1=20
monkeybefore recurse: count=0, str=0
monkey before recurse: count=1, str=4
monkey before recurse: count=2, str=8
monkey before recurse: count=3, str=12
monkey before recurse: count=4, str=16
monkey before recurse: count=5, str=20
monkey after recurse: count=5, str=20
monkey after recurse: count=4, str=16
monkey after recurse: count=3, str=12
monkey after recurse: count=2, str=8
monkey after recurse: count=1, str=4
monkeyafter recurse: count=0, str=0
Update: Immediately after posting, I realize that I didn't need two functions to illustrate it, the count variable is already localized, so the first function was enough. Ah, well.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.