Newbie trying to understand some perl code.

E

ed

I have the following code and I'm trying to understand it a little
better, can anyone help?

#!/usr/bin/perl

$startdir = "/lib";

$level = 0;

list_dirs($startdir,$level);

sub list_dirs(){
my $dir = shift (@_);
my $lev = shift (@_);


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

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

foreach $file (@files){
if(-d "$dir/$file"){
spaces($lev);
print "$file\n";
list_dirs("$dir/$file",$lev+1);
}
}

}

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


I understand in the foreach statement that it is calling the sub
routine "spaces($lev),
I'm trying to understand the sub routine "spaces", how is it making my
subdirectories indent?
 
J

John Bokma

[ ... ]
spaces($lev);
print "$file\n";
list_dirs("$dir/$file",$lev+1);

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


I understand in the foreach statement that it is calling the sub
routine "spaces($lev),
I'm trying to understand the sub routine "spaces", how is it making my
subdirectories indent?

By the level parameter, which is increased in every recursive call ( $lev+
1)

horrible code by the way.
 
S

Sherm Pendley

ed said:
I have the following code and I'm trying to understand it a little
better, can anyone help?

#!/usr/bin/perl

$startdir = "/lib";

$level = 0;

list_dirs($startdir,$level);

sub list_dirs(){
my $dir = shift (@_);
my $lev = shift (@_);


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

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

foreach $file (@files){
if(-d "$dir/$file"){
spaces($lev);
print "$file\n";
list_dirs("$dir/$file",$lev+1);
}
}

}

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


I understand in the foreach statement that it is calling the sub
routine "spaces($lev),
I'm trying to understand the sub routine "spaces", how is it making my
subdirectories indent?

Inside of spaces(), the arguments that were passed in are found in the
array @_. The shift() at the beginning pops the first one off (which was
passed in as $lev), and assigns that value to $num.

Then it enters a for() loop, and prints $num spaces. Because there's no
newline "\n" after each space, they all appear on one line, before the
filename "$file\n".

You should have a look at "perldoc perlintro". From the first paragraph:

"This document is intended to give you a quick overview of the Perl
programming language, along with pointers to further documentation. It
is intended as a "bootstrap" guide for those who are new to the language,
and provides just enough information for you to be able to read other
peoples' Perl and understand roughly what it's doing, or write your own
simple scripts."

In case you're wondering what "perldoc perlintro" means, it's referring
to Perl's built-in documentation. Just type "perldoc perlintro" at a shell
prompt. Or, if you're using ActiveState's Perl for Windows, look in the
Start menu for HTML-i-fied docs. And finally, if you're using a Mac, you
might want to give my own ShuX app a try.

Other good starting points:

perldoc perl

http://learn.perl.org

sherm--
 
J

John Bokma

ed said:
One other question, the print line, why is it print " "?

is explained here:
<http://groups.google.com/support/bin/answer.py?answer=14213>

" " is one space, so it prints $lev spaces, $lev starts at zero.

I would remove the spaces call, and replace:

print "$file\n";


with

print ' ' x $lev, "$file\n"; #(untested)

or probably even:

print ' ' x $lev, "$file\n"; #(untested)

to get a indentation of 4 spaces/level

(which could also be written as:

print ' ' x ( $lev * 4 ), "$file\n";

)
 
S

Sherm Pendley

John Bokma said:
horrible code by the way.

What do you expect from a beginner? Anyway, he gave it a good try and posted
his code with a specific question attached - that's a hell of a lot better
than a lot of people manage to do.

sherm--
 
S

Sherm Pendley

John Bokma said:

No, it's not. It's certainly good, I'd even say critical, info for
Google Groups users. But it's nothing at all to do with the above
question, much less an explanation.

Ed, the above link explains how you can post from Google Groups
the way that everyone else here posts, including enough of the post
we're following up for our own comments to make sense. That's good
to do because not every uses Google Groups; messages may get dropped
from some servers, may not have arrived yet, etc. If you quote the
relevant parts of the preceding message (as I've done above), then
someone who hasn't necessarily seen that message on its own have at
least a chance of following the conversation.

sherm--
 
J

John Bokma

Sherm Pendley said:
What do you expect from a beginner?

It's not his code, or at least so I understand. And I think it's a good
thing to tell a beginner that the code he/she got, is not good.
Anyway, he gave it a good try and
posted his code with a specific question attached - that's a hell of a
lot better than a lot of people manage to do.

True.
 
E

Euclid Uranium

I have the following code and I'm trying to understand it a little
better, can anyone help?

Hey, Perl is a write-only language. It's not meant to be read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top