File::Find is a very useful module, but it isn't particulary easy to use. Here is a snippet which does what File::Find does without the module. Modify to your hearts desire.

Note: since this is so completely against the spirit of CPAN etc, I'm expecting your flames and I've put my asbestos underwear on ready ;-)

Note2: I wrote this snippet originally to delete an nearly infinite directory of files which a user created on the server. 'rm -rf' wouldn't and I didn't think of File::Find until too late ;-)

Note3: In response to merlyn's post below, you can uncomment the lines marked if you want a symlink safe version. Though recursive symlinks are usually a bad idea in any case!

#!/usr/bin/perl -w # # Demonstration of going it alone without File::Find use strict; use Cwd; die "Syntax: $0 <dir>" unless @ARGV == 1 && -d $ARGV[0]; find(0, $ARGV[0]); exit; sub find { my ($level, $dir) = @_; my ($back) = cwd() or die "Failed to read current working directory"; chdir($dir) or die "Couldn't cd to $dir"; opendir(DIR, ".") or die "Failed to opendir()"; my @files = grep { $_ ne '.' && $_ ne '..' } readdir(DIR); closedir(DIR) or die "Failed to closedir()"; for my $file (@files) { # If you want a symlink safe version write this # lstat($file); # if (-d _) if (-d $file) { # Do something with a directory print ' ' x $level, "Dir: '$file'\n"; # ...then recurse if you want to find($level+1, $file); } else { # Do something with a file (don't recurse) print ' ' x $level, "File: '$file'\n"; } } chdir $back or die "Failed to chdir $back"; }

In reply to Going it alone without File::Find by ncw

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.