QUOTE(n00bc0der @ 28 May, 2009 - 02:05 PM)

So I'm writing some code in perl where I want to use a sub-routine that recursively calls it self to find a list of directories & subdirectories from a given directory. I want to collect all of these directories into one large array.
My question is: How can I recursively call a sub-routine to populate an array?
By calling it from within itself. You don't have to do anything special in Perl to enable recursion.
QUOTE(n00bc0der @ 28 May, 2009 - 02:05 PM)

Here's the code I have so far:
CODE
@dirlist = ();
#Used to detect all directories in a given original directory
sub getfulldirectorylist {
my $dirname = @_;
my @templist = `cd $dirname; ls -d */`;
if (@templist) {
foreach $templist (@templist) {
push(@dirlist, $templist);
foreach $dirlist (@dirlist) {
getfulldirectorylist($dirlist);
}
}
}
}
It looks to me like you've set up a fairly complex infinite loop here. When you find the second directory, the way your loops are set up will cause them to run getfulldirectorylist again on the first directory, which will call gfdl on the first directory again, etc. (I haven't run it to verify this, but pushing each directory onto @dirlist, then calling recursively for every entry in @dirlist (including those that have already been processed) should have that effect.)
Also, even if it were to terminate, you haven't told it what to return, so it would just return the value of the last statement processed, which is unlikely to be the result you're looking for.
Seriously, if you're going to go out to the shell for this anyhow, the easiest way to accomplish it would be to have ls handle the recursion itself:
CODE
sub getfulldirectorylist {
my $dir = shift;
return `cd $dir; ls -Rp | grep /\$`;
}
QUOTE(n00bc0der @ 28 May, 2009 - 04:19 PM)

QUOTE
However, you should take a look at the File::Find module - as it will do all this for you
I'm unable to use any of these modules because of the product I'm producing. It's a complicated/confidential thing.
1) Everything in CPAN is under some open source license or other and the vast majority (including File::Find) is licensed under the same terms as Perl itself. If you can use Perl, then using CPAN modules under the same license would raise no additional licensing or confidentiality issues. Even if it were one of the few CPAN modules licensed only under GPL, there would be no licensing or confidentiality issues raised unless you distributed the code in a compiled binary form; as the default for Perl programs is to distribute in source form, GPL would still be unlikely to cause any actual issues.
2) girasquid is correct. File::Find is a core module:
http://perldoc.perl.org/index-modules-F.html If you have Perl installed, then you already have File::Find installed also, unless someone made a deliberate effort to build a Perl distribution without it.