Win has asked for the wisdom of the Perl Monks concerning the following question:

This program and variants of is frustrating me as it either takes files that are not in the location I want it to look in or it doesn't find the files that I want and that exist in the directory I have told it to look in. Help appreciated.
#! perl -w scipt use strict; use warnings; use File::Find::Rule; use File::Find::Rule; # find all the subdirectories of a given directory my $directory = "U:/2004_2005/Application/Development/Useful_methods/" +; my $directory_label = "U:/2004_2005/Application/Development/Useful_met +hods"; my @subdirs = File::Find::Rule->directory->in( $directory_label ); print @subdirs; #exit; foreach (@subdirs){ print "Hi\n"; # if ($_ !~ /$directory_label|$directory/){ print $_; print "\n\n"; my $directory_current = $_; # print cwd(); # print \n; my $rule = File::Find::Rule->new; $rule->directory( '$directory_current' ); $rule->file; $rule->name( '*.*' ); #$rule->name( qr/^[^.]{118,1000}$/); # # ); # (\.[^.]{0 +,20})?$/ ) ; # $rule->name( qr/^.{20,1000}$/); my @files = $rule->in( @INC ); foreach(@files){ print $_; } # } }
The following section is not working properly:
my @files = $rule->in( @INC ); foreach(@files){ print $_; }

Replies are listed 'Best First'.
Re: use File::Find::Rule
by tirwhan (Abbot) on Jul 31, 2009 at 17:04 UTC

    That's what happens when you just copy and paste a module example without understanding what is going on. Read the File::Find::Rule documentation, it explains this very clearly. You might also want to search perlvar for an explanation on what @INC is/contains.


    All dogma is stupid.
Re: use File::Find::Rule
by MidLifeXis (Monsignor) on Jul 31, 2009 at 16:55 UTC

    What is it giving you, and what are you expecting it to give you?

    --MidLifeXis

    The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post

Re: use File::Find::Rule
by Anonymous Monk on Jul 31, 2009 at 17:03 UTC
    Do you do care/realize @INC and @subdirs are different?
      I was hoping that the @INC would contain the files with full directory paths. Which is does, but not the files I was expecting.
Re: use File::Find::Rule
by Marshall (Canon) on Aug 02, 2009 at 02:37 UTC
    I see that you are a Windows user. If you are going to use that first line, "#! perl -w scipt", it should look like this: #!/usr/bin/perl -w. On Unix this says to run the program at /usr/bin/perl (ie Perl) with the -w option. On Windows, this path is ignored and Windows only uses the -w option. This takes the place of "use warnings;" in the code.

    I am just going from this comment:
    # find all the subdirectories of a given directory.

    Before learning about fancy modules, I would recommend learning the basics. The code that you need is not hard.

    #!/usr/bin/perl -w use strict; use File::Find; my $start_dir = "C:/"; find (\&print_all_directories, "$start_dir"); sub print_all_directories { return if !-d; my $full_dir_path = $File::Find::name; print "$full_dir_path\n"; }
    find()in module File::Find, visits all files below the directory specified, calling the specified subroutine on each file. A directory is also a file! If the file isn't a directory, print_all_directories() just returns, if this is a directory, we figure out the full path name and print it.