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.


In reply to Re: Hi Monks could you pls help Perl dummy user by roboticus
in thread Hi Monks could you pls help Perl dummy user by sibyurik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.