gsoft
12-07-2004, 01:18 AM
Ok ive been reading Learning Perl damn good book, not only an interesting read ive learnt something :D
Anyway from the book they never had a recursive directory so I decided why not try and make one. It may not be efficient im sure theres a Perl Module out there for this to make a it a lot faster.
#!/usr/bin/perl
sub read_directory {
my $directory = $_[0];
print "$directory\n\n";
opendir DIR, $directory or die "Cannot open Directory $!";
my @files = readdir DIR;
for (@files) {
next if $_ eq "." or $_ eq "..";
&read_directory("$directory/$_") if -d "$directory/$_";
print "$directory/$_\n" if !-d "$directory/$_";
}
closedir DIR;
}
my $directory = "/var/www/web";
&read_directory($directory);
At the moment it requires the directory to be specified in the file. Is there anyway to make this faster, its pretty fast at the moment but I am sure it can be slightly faster, in the end I am going to be making a Backup Program for a directory so it needs to be as fast as possible.
Also any feedback on what you think of my first program done without a book would be nice thanks in advance :)
Anyway from the book they never had a recursive directory so I decided why not try and make one. It may not be efficient im sure theres a Perl Module out there for this to make a it a lot faster.
#!/usr/bin/perl
sub read_directory {
my $directory = $_[0];
print "$directory\n\n";
opendir DIR, $directory or die "Cannot open Directory $!";
my @files = readdir DIR;
for (@files) {
next if $_ eq "." or $_ eq "..";
&read_directory("$directory/$_") if -d "$directory/$_";
print "$directory/$_\n" if !-d "$directory/$_";
}
closedir DIR;
}
my $directory = "/var/www/web";
&read_directory($directory);
At the moment it requires the directory to be specified in the file. Is there anyway to make this faster, its pretty fast at the moment but I am sure it can be slightly faster, in the end I am going to be making a Backup Program for a directory so it needs to be as fast as possible.
Also any feedback on what you think of my first program done without a book would be nice thanks in advance :)