I have been learning perl for the last two months and now I have presented myself with a problem. I decided to write a Perl script that will basically search my linux box for all files with the suid or sgid buit set starting from the root directory (/). I already know that there will involve some sort of recursion I just can't figure out a good approach to this:
chdir("/")||die "Error:$!\n"; opendir(ROOT_DIR,"/")|| die "Error:$!\n"; foreach $dir_cont (sort readdir(ROOT_DIR)){ next if $dir_cont =~s/^\.\.?$/; if (-f $dir_cont){ log_sbit($dir_cont) if (-u $dir_cont || -g $dir_cont); } elsif (-d $dir_cont && opendir(SUB_DIR,"$dir_cont"){ do_stuff....... }
This is where I get stuck .... I guess I need some kind of recursion or a way to search all directories and their respective sub-directories. Also I need a way for my script to remember where each file (if it has the suid or sgid bit) is located so the log file has:
/usr/local/bin/some_file suid /var/log/.hidden/file sgid /home/greco/bin/be_greco sgid
I know that there are various modules out there (File::Find) that I can utilize, but since I am still in my learning stages, I would like to go on this journey of reinventing the wheel so to speak, so I can better understand perl and learn some things along the way :) All help will be much appreciated.

In reply to Recursion by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.