in reply to How to perform recursion in any OS?
Update:See following posts.. evidently some "system" Perls on Unix machines may not have all of the Core modules! Users are well advised to install their own Perl environment. I do most of my work nowadays on Windows (which doesn't come with Perl) so the issues are different. The code below is multi-platform (I tested on Windows), but you do need File::Find.
Pay attention to the error messages for invalid command line input.
#!/usr/bin/perl use strict; use warnings; use File::Find; my $path = shift; # what you expect to happen! # @ARGV contains one thing which is # a path to a directory. This shift removes # that path from @ARGV and it goes to $path # Now check if the "normal case" is correct or not... + usage("no directory specified") if !defined $path; usage("too many arguments. Only one dir allowed") if (@ARGV !=0 ); usage("$path does not exist") if (not -e $path); usage("$path is not a directory") if (not -d $path); sub usage { my $msg = shift; print STDERR "$msg\n"; exit (1); # exit(0) is success, all other values # mean a failure as per common practice } find(\&each_file, $path); # each_file() is called for every # file underneath $path. # Note that a directory is a special # kind of a file. -f tests if this name # is a "simple file". sub each_file { # your code goes below... # $File::Find::name is the current file in this recursive # descent underneath the directory $path # if (-f $File::Find::name) #a simple file (NOT ., .., Or any other d +irectory) { print "$File::Find::name\n"; #current file name # I would make an HTML sub to call here for that name.. } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to perform recursion in any OS?
by hippo (Archbishop) on Dec 09, 2016 at 09:57 UTC | |
by CountZero (Bishop) on Dec 09, 2016 at 17:00 UTC |