Re: don't understand working script



On 6/28/07, Amichai Teumim <amichai@xxxxxxxxxx> 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

.



Relevant Pages

  • Re: help emailing this script
    ... "Remember to pass two arguments to this subroutine ... sub WriteTextFile ... Below is the script I have now created ... Dim objWMIService, objItem, colItems ...
    (microsoft.public.scripting.vbscript)
  • Re: Newbie: Looking for comments on this (working) script
    ... >> This function will only accept two scalar values. ... > Basically to clarify your intitial question, this prototype doesn't do ... declares f1 to be a subroutine that will accept a single scalar followed ... sub foo ...
    (comp.lang.perl.misc)
  • Re: free source bbs
    ... MB> ignoring the prototype of a subroutine, ... the ampersand also has the unusual property of providing the ... remark means if you are using a sub with a prototype (which is basically ...
    (comp.lang.perl.misc)
  • Re: passing code inside function
    ... > sub a { ... turns the subroutine into a list operator, ... If you were to call awith the prototype set up then ... is different entirely from an anonymous subroutine. ...
    (perl.beginners)
  • Re: Dynamically calling a subroutine
    ... > I have a main program which will call a subroutine, say "S", belonging ... all modules are included into the main script for ... sub hello ...
    (perl.beginners)