in reply to Help taming File::Find

Do not nest the named subs. Move the wanted sub outside of the get_files sub, and make @myfiles a global, or make wanted an anonymous sub, e.g., find(sub { ... }, @directories).

Replies are listed 'Best First'.
Re^2: Help taming File::Find
by tphyahoo (Vicar) on Jun 13, 2005 at 17:19 UTC
    Yup, that did the trick -- much obliged, runrig. Fixed code is:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use File::Find; my $dir = shift or die "bitte html suchen/ersetzen verzeichnis eingebe +n\n"; opendir(DIR, $dir) or die "html suchen/ersetzen verzeichnis existiert +nicht: $dir"; closedir(DIR); #works, but with warning: #Variable "@myfiles" will not stay shared at fileFindTroubleshooting.p +l line 34. my @files = get_files($dir); foreach (@files) { print "file: $_\n" } sub get_files { my $directory = shift; opendir (DIR, $directory) or die "couldn't open directory: $direct +ory"; my @myfiles; my @directories_to_search = ("$directory"); find( sub { my $name = $File::Find::name; if ( $name =~ /html$/i && ! ( $name =~ /\.backup\.before/i + ) ) { $name =~ s|\\|/|; # substitute forward slash for back +slashes, icky but don't know a better way. #print "name: $name\n"; push @myfiles, $name; } }, @directories_to_search); return @myfiles; }