« Timing Is Everything | Main | A shorter filesystem recurser in Perl »
August 26, 2006
Recursivly print directories in Perl...with no modules
Just for fun I came up with some Perl to recursively print out a directories files and child directories. Why am I posting it? It's not that it's all that difficult or obscure, I just feel like it, and when I did a web search for code on this that doesn't use the File::Find Perl module, I was surprised as to how few results there were. So in the interest of reinventing the wheel a bit, here's what I came up with.
#!/usr/bin/perl use strict; use warnings;printdir( $ARGV[0] );
sub printdir
{
my $dir = $_[0] ? $_[0] : '.';
my $handle = ();
my @dirs = ();
my $i = ();
print "Directory: $dir\n";
opendir( $handle, $dir );
my @items = readdir( $handle );
closedir( $handle );
shift @items; #Get rid of .
shift @items; #Get rid of ..
@items = sort( @items );
while ( $i = shift( @items ) )
{
if ( -d "$dir/$i" )
{
push( @dirs, $i );
next;
}
print "$dir/$i\n";
}
printdir( "$dir/$i" ) while ( $i = shift( @dirs ) );
}
Posted by Casey at August 26, 2006 07:24 PM