Having Trouble Recursing a Function

Discussion in 'Perl Misc' started by Mark Healey, Feb 24, 2005.

  1. Mark Healey

    Mark Healey Guest

    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
     
    Mark Healey, Feb 24, 2005
    #1
    1. Advertising

  2. Jim Gibson <> wrote:
    > In article <>, Mark Healey
    ><> wrote:
    >
    >> Can anyone tell me why the following only goes one level deep in the



    >> my @list = readdir(DIRHANDLE);



    >> if(-d $_)



    > 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...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 25, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. snowdy

    Recursing code problem

    snowdy, Aug 29, 2003, in forum: C Programming
    Replies:
    19
    Views:
    1,198
    Kevin D. Quitt
    Sep 2, 2003
  2. Scott Carlson

    recursing through files in a folder

    Scott Carlson, Oct 1, 2004, in forum: Python
    Replies:
    3
    Views:
    378
    Mirko Zeibig
    Oct 1, 2004
  3. Replies:
    4
    Views:
    347
  4. Henrik Goldman

    Recursing macro preprocessing?

    Henrik Goldman, Oct 21, 2006, in forum: C++
    Replies:
    4
    Views:
    388
    Kaz Kylheku
    Oct 22, 2006
  5. Randy

    StackOverFlowException When Recursing Page Controls

    Randy, Jan 18, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    145
    Randy
    Jan 19, 2006
Loading...

Share This Page