You're looping on the first item in the list which will be a '.'

Works better if you add next if $item eq '.' or $item eq '..'; after the foreach

readdir gives you the names in the directory... not path names, so when you call the printdir recursively you'll need to prefix the current directory to each element of the list. (Naturally you'll need to change your '.' test accordingly.

Anyway this seems to work on my machine.

#!perl -w
use strict;
use Carp;

printdir(@ARGV);

sub printdir {
    my $item;
    foreach $item(@_) {
      next if $item =~ /\.{1,2}$/;
      if (-d $item) {
        print "$item\n";
        opendir(SUBDIR, $item) or croak "Can't open directory :$!";
        my @subdir_items = readdir(SUBDIR);      #+
        closedir(SUBDIR);                        #+  
        printdir(map {"$item/$_"}@subdir_items); #+
      }
    }
  }

Oh, by the way the equivalent in my previous post should have been

perl -Mstrict -MFile::Find -wle 'find sub{print $File::Find::dir if -d},@ARGV' .
but you saw that already, didn't you :)

Update: Just saw merlyn's post. The above ran on Win32 because (as he says) the Windows port recognizes forward slashes as valid directory separators. I don't know whether this is an issue on other non unix ports. Still, I agree with him, it's a lot safer here to stay with the standard.

Update 2: The '.' test is broken: it will match any file ending with a '.'. You'd need to split off the file name from the path, so you might as well use File::Spec... Oh good grief, why did I ever post this code? This'll teach me to shut up :(


In reply to Re: As usual by Tyke
in thread Recursive directory scanning by pwhysall

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.