From: "Amichai Teumim" <[EMAIL PROTECTED]>
> Can someone explain to me what this script really does? I mean I see that it
> lists dir within dir. But what is the code doing? For example all the blue
> highlighted stuff, what is it doing?
There is no highlighting in a plain text email!
> #!/usr/bin/perl
missing
use strict;
use warnings;
> $startdir = "/lib";
> $level = 0;
Should be
my $startdir = "/lib";
my $level = 0;
> list_dirs($startdir,$level);
>
> sub list_dirs(){
> my $dir = shift (@_);
> my $lev = shift (@_);
Would be better written
my ( $dir, $lev) = @_;
> opendir(TOP,$dir);
> my @files = readdir(TOP);
> closedir(TOP);
>
> shift(@files);
> shift(@files);
Here it removes the '.' and '..' "directories" from the list.
> foreach $file (@files){
foreach my $file (@files){
Without the "my", you are reusing the global variable $file, that all
the instances of the list_dirs() subroutine that you call recursively
to traverse the filesystem share the same variable. In this case it
doesn't cause a visible error since the recursive call is the last
thing you do in the loop, but still you should be careful with this!
Unless you really really need to use a global you should declare all
your variables with "my". Especially within recursive subroutines.
> if(-d "$dir/$file"){
"if $dir/$file is a directory"
> spaces($lev);
prints the $lev spaces. Could be written as
print ' ' x $lev;
> print "$file\n";
> list_dirs("$dir/$file",$lev+1);
> }
> }
>
> }
The script traverses the filesystem, starting in /lib and prints all
directories and subdirectories and subsubdirectories etc.
Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/