Newbie trying to understand some perl code.

Discussion in 'Perl Misc' started by ed, Nov 23, 2005.

  1. ed

    ed Guest

    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?
     
    ed, Nov 23, 2005
    #1
    1. Advertising

  2. ed

    John Bokma Guest

    "ed" <> wrote:


    [ ... ]

    > 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.


    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 23, 2005
    #2
    1. Advertising

  3. ed

    ed Guest

    One other question, the print line, why is it print " "?
     
    ed, Nov 23, 2005
    #3
  4. "ed" <> writes:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Nov 23, 2005
    #4
  5. ed

    John Bokma Guest

    "ed" <> wrote:

    > 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";

    )


    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 23, 2005
    #5
  6. John Bokma <> writes:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Nov 23, 2005
    #6
  7. John Bokma <> writes:

    > "ed" <> wrote:
    >
    >> One other question, the print line, why is it print " "?

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


    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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Nov 23, 2005
    #7
  8. ed

    ed Guest

    I'd like to thank everyone for their help tonight, I really appreciate
    it.
     
    ed, Nov 23, 2005
    #8
  9. ed

    John Bokma Guest

    Sherm Pendley <> wrote:

    > John Bokma <> writes:
    >
    >> horrible code by the way.

    >
    > 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.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 23, 2005
    #9
  10. ed

    John Bokma Guest

    "ed" <> wrote:

    > I'd like to thank everyone for their help tonight, I really appreciate
    > it.


    To make sure you get help in the future, learn how to quote, thanks.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 23, 2005
    #10
  11. ed

    Dr.Ruud Guest

    Dr.Ruud, Nov 23, 2005
    #11
  12. > 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.
     
    Euclid Uranium, Nov 24, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Lenz
    Replies:
    6
    Views:
    476
    jeffc
    Dec 28, 2004
  2. Bob

    newbie-trying to understand qsort

    Bob, Dec 2, 2005, in forum: C Programming
    Replies:
    1
    Views:
    393
  3. bufferOverflow

    Trying to understand some C/C++ code

    bufferOverflow, Jul 25, 2007, in forum: Java
    Replies:
    9
    Views:
    391
    shakah
    Jul 25, 2007
  4. Patrick

    Trying to understand Perl

    Patrick, Nov 30, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    109
  5. walterbyrd
    Replies:
    18
    Views:
    277
    C.DeRykus
    Dec 16, 2008
Loading...

Share This Page