Problem is, because all of the variables are global, it doensn't unwind...

you have scope issues
here is a quick example to guide you, notice the use of my

#!/usr/bin/perl use strict; my $current = '/usr/local'; my $dir_start = '/home'; print "Current is '$current', starting with '$dir_start'\n\n"; recurse_directory( $dir_start ); print "\nDone.\n\n"; sub recurse_directory { ## variables my $current; my @local_list; ## get the dir we are in $current = $_[0]; ## get contents ## no "." or ".." dirs opendir( CURRENTDIR, "$current" ) or die "Can not open $current: $!"; @local_list = grep !/^\.\.?$/, readdir CURRENTDIR; closedir( CURRENTDIR ) or die "Can not close $current: $!"; ## now loop over the contents foreach my $x ( @local_list ) { my $file = $current . '/' . $x; recurse_directory( $file ) if ( $file -d ); print "$file\n"; } }
there is quite a bit wrong with this snippet, as i am sure will be pointed out, but this should help you with your scope problems.

the in depth differences between global, local, and my variables may be more than you are looking for, but a good place to start is page 189 of the Camel book, 2nd edition( no idea on the page in 3rd edition, sorry )

Will perl for money
JJ Knitis
(901) 756-7693
gt8073a@industrialmusic.com


In reply to Re: recursive sub by gt8073a
in thread recursive sub by Anonymous Monk

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.