Re: don't understand working script



Amichai Teumim wrote:
I have this script, If you run it you can see how it nicely idents the
directories. I don't understand everything in this script.

[ SNIPPED an example of *BAD* code ]


Your example would be better written as:

#!/usr/bin/perl
use warnings;
use strict;

my $startdir = '/lib';
my $level = 0;

sub list_dirs {
# remove first argument from @_
my $dir = shift;
# remove second argument from @_
my $lev = shift;

opendir TOP, $dir or die "Cannot open '$dir' $!";
# get all directory names except '.' and '..'
my @files = grep -d "$dir/$_" && !/\A\.\.?\z/, readdir TOP;
closedir TOP;

foreach my $file ( @files ) {
print ' ' x $lev, "$file\n";
list_dirs( "$dir/$file", $lev + 1 );
}
}


list_dirs( $startdir, $level );


__END__



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages