On 6/28/07, Amichai Teumim <[EMAIL PROTECTED]> 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. Please see my
comments.

#!/usr/bin/perl

$startdir = "/lib";

$level = 0;    #WHAT DOES THIS DO?

It assigns 0 to the scalar variable $level.


list_dirs($startdir,$level); #WHAT DOES THIS DO?

it calls the subroutine &list_dirs with the arguments $startdir and $level


sub list_dirs(){

This is a misuse of prototypes.  it should be

sub list_dirs {

Luckily, if you want to call it that, the misuse has no effect on the
program because the call to the subroutine occurs before the
definition of the subroutine, thus causing the code to ignore the
prototype.

  my $dir  = shift (@_);  #WHAT DOES THIS DO?
  my $lev = shift (@_);   #WHAT DOES THIS DO?


@_ is an array.  In this context it holds the arguments passed to
list_dirs.  So the scalar $dir is being assigned the contents
$startdir variable from above.  Likewise $lev is being assigned the
contents of $level.



  opendir(TOP,$dir);
  my @files = readdir(TOP);
  closedir(TOP);

  shift(@files);
  shift(@files);

  foreach $file (@files){
    if(-d "$dir/$file"){
        spaces($lev);   #WHAT DOES THIS DO?

calls the spaces subroutine with the argument $lev

        print "$file\n";
        list_dirs("$dir/$file",$lev+1); #WHAT DOES THIS DO?

The subroutine is calling itself.  This is called recursion.  It is a
form of looping. You might want to read
http://en.wikipedia.org/wiki/Recursion_(computer_science)

    }
  }

}

#WHAT DOES THIS WHOLE SECTION DO?


sub spaces(){
my($num) = shift(@_);
for($i=0;$i<$num;$i++){
print " ";
}

}

It is poorly written and poorly indented, but this subroutine prints
out the number of spaces passed as an argument.  It also is misusing
prototypes.



Thanks

Amichai


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to