Having Trouble Recursing a Function

M

Mark Healey

Can anyone tell me why the following only goes one level deep in the
directory tree?

I'm stumped


#!/usr/bin/perl

use strict;
use Cwd;

my $cdir;
my @files;

$cdir = getcwd();

doDir($cdir);

foreach(@files)
{
printf("$_\n");
}

exit;

sub doDir
{
my $dir = $_[0];
printf("####$dir####\n");
my $fname;
opendir(DIRHANDLE, $dir);
my @list = readdir(DIRHANDLE);
closedir(DIRHANDLE);
foreach(@list)
{
chomp;
if(-d $_)
{
unless(/\.\.?\z/)
{
$fname=$dir.'/'.$_;
doDir($fname);
}
}
else
{
if(/\.mp3\z/)
{
$fname=$dir.'/'.$_;
push(@files, $fname);
}
}
}# end foreach(@list)
}# end doDir()

TIA
 
T

Tad McClellan

Jim Gibson said:
You
need to check "$dir/$_" instead.


Just like it says in the documentation for the function that
the OP is using!

Asking hundreds of people to read the docs to you is not very nice...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top