in reply to How to perform recursion in any OS?

Here is some code that may help you...
File::Find is a module that is most likely included in your Perl distribution without having to install anything.
Update: File::Find is a core module, so it is for sure in your installation. There are other similar modules. File::Find does the recursion without you having to think much about it.

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
    File::Find is a core module, so it is for sure in your installation.

    It would be nice if that were true. Unfortunately, some (many?) vendors choose to provide the perl binary separately from the core modules. This means that it is not only possible to have perl installed on a system without the core modules but in some cases (yes, I'm looking at you, RedHat) it is the common scenario. Thankfully those core modules are usually available elsewhere in the distribution and it is usually a simple matter of using the vendor's package manager to install them.

    This point has been made a few times before. See for example, Re: Why not include version.pm

      Never expect your "system" Perl to do anything else but system-related tasks and do not rely upon it for anything else.

      Install your own Perl (perlbrew is ideal for this) and use that for your tasks. That way you are sure to have a modern/recent Perl that contains the modules and tools you need.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics