Having Trouble Recursing a Function

From: Mark Healey (die_at_spammer.die)
Date: 02/24/05


Date: Thu, 24 Feb 2005 22:40:44 GMT

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

-- 
Mark Healey
marknews(at)healeyonline(dot)com


Relevant Pages

  • Variable remaining undef in one place but not another.
    ... the cwd on to part that starts the HTML, so that the cwd can be used in ... use warnings; ... use strict; ... sub htmlStart{ ...
    (comp.lang.perl.misc)
  • Re: Cron Job & Perl
    ... Must be something different in the environment then, cwd, env vars, etc... ... use strict; ...
    (comp.lang.perl.misc)
  • Re: Dynamic directory handles?
    ... > If your code runs with strict, ... > has higher precendence than '=' which is higher than 'or'. ... > my= shift or ''; ... > and thus $cdir may still be undefined. ...
    (comp.lang.perl.misc)
  • Re: Taint mode question
    ... simply ECHOs a text string and is placed in my CWD. ... The program as it is below works as expected, however if swap the comments on the two $ENVlines it no longer works and reports an insecure $ENV. ... that the return value of getcwd() is tainted. ...
    (comp.infosystems.www.authoring.cgi)
  • Re: /$RE{URI}{HTTP}{-keep}/;
    ... On Tuesday 15 February 2005 11:06 am, Brian Volk wrote: ... > use warnings; ... > use strict; ... > use Cwd; ...
    (perl.beginners)