Re: don't understand working script
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Thu, 28 Jun 2007 09:05:06 -0700
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
.
- References:
- don't understand working script
- From: Amichai Teumim
- don't understand working script
- Prev by Date: Re: missing curly - brain fried
- Next by Date: SOLVED - Re: Tie::Handle::CSV
- Previous by thread: Re: don't understand working script
- Next by thread: missing curly - brain fried
- Index(es):
Relevant Pages
|