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

I'm trying to use File::Find on a directory I know exists. The script below lists the directory just fine, but the find() gives me invalid top directory at C:/Perl/lib/File/Find.pm line 562. Checking Google Groups and Super Search didn't help me. What's going on?
#!perl use strict; use File::Find; my $archive_file = shift; my $source_dir = shift; print "Calling Find on <$source_dir>\n"; system("dir $source_dir"); find({ no_chdir => 1, wanted => sub { print STDERR "*** Doing $_ ***\n"; if (-f) { print "...Adding $_\n" } elsif (-d _) { print "Adding dir $_...\n" } }, $source_dir }) or die "$0: $!\n";

We're not really tightening our belts, it just feels that way because we're getting fatter.

Replies are listed 'Best First'.
Re: File::Find blind
by Aragorn (Curate) on Jun 25, 2004 at 18:41 UTC
    You put the $source_dir variable inside the hashref which is the first argument. $source_dir should be the second argument to find:
    find({ no_chdir => 1, wanted => sub { print STDERR "*** Doing $_ ***\n"; if (-f) { print "...Adding $_\n" } elsif (-d _) { print "Adding dir $_...\n" } } }, $source_dir) or die "$0: $!\n";
    Arjen
Re: File::Find blind
by runrig (Abbot) on Jun 25, 2004 at 18:38 UTC
    You've got a problem with curly brace placement. The closing brace belongs before $source_dir (and before the comma), not after.
Re: File::Find blind
by Limbic~Region (Chancellor) on Jun 25, 2004 at 18:31 UTC
    Roy Johnson,
    From looking at the source, this error ocurrs if the directory is not defined. Since you know that it is defined, perhaps it isn't where it needs to be?
    find({ no_chdir => 1, wanted => sub { print STDERR "*** Doing $_ ***\n"; if (-f) { print "...Adding $_\n" } elsif (-d _) { print "Adding dir $_...\n" } }}, $source_dir ) or die "$0: $!\n";

    Cheers - L~R

    Provided code since wording wasn't clear
Re: File::Find blind
by Roy Johnson (Monsignor) on Jun 25, 2004 at 18:38 UTC
    Found it. The closing brace is misplaced. D'oh!
    Thanks anyway, to anyone who tried it out.

    We're not really tightening our belts, it just feels that way because we're getting fatter.