Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Going it alone without File::Find

by ncw (Friar)
on Sep 18, 2000 at 23:39 UTC ( [id://33017]=CUFP: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
RE: Going it alone without File::Find
by merlyn (Sage) on Sep 18, 2000 at 23:41 UTC
      The way to fix that would be to change the test to
      if (-d $file && ! -l $file)
      Also possibly change the syntax check to:
      die "Syntax: $0 <dir>" unless @ARGV == 1 && (-d $ARGV[0] && ! -l $ARGV[0]);


      If you're going to say the code is bad, at least suggest a fix. Fortunately ZZamboni already has.
        My "fix" is the normal fix. Use File::Find, or suggest patches to it if it doesn't work the way you want. That way I don't have to review every line of your code in code-review to find out if you've broken it on some platforms, or introduced a security hole.

        The cost of revinventing the wheel is not just in development, but in every phase afterward: testing, documenting, production, and maintenance.

        -- Randal L. Schwartz, Perl hacker

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://33017]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-05-27 06:34 GMT
Find Nodes?
    Voting Booth?

    No recent polls found