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

I admit I'm guessing here! I'm trying to put my files into an array and use this array later in the script:
my @array = find(\&collect_file, $dirphoto); for (@array) { print "$_<br>"; } } sub collect_file { my @array; push (@array, $File::Find::name); }

Replies are listed 'Best First'.
Re: return an array from File::Find
by jwkrahn (Abbot) on Mar 30, 2008 at 20:54 UTC
Re: return an array from File::Find
by CountZero (Bishop) on Mar 30, 2008 at 21:01 UTC
    Your lexical @array within the collect_file-subroutine does not exist outside of that subroutine. It has no relationship whatsoever (other than that they are named the same which is quite confusing) with the lexical @array you declared at the beginning of the script.

    The find function does not return the result of the search. May be you were thinking of File::Find::Rule where the find function does return a list of files found. Your script would then become:

    use strict; use File::Find::Rule; my @array = File::Find::Rule->new->in($dirphoto);

    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

Re: return an array from File::Find
by ForgotPasswordAgain (Vicar) on Mar 30, 2008 at 20:21 UTC

    Why do you insist on returning the array from find?

    my @array; find(sub { push @array, $File::Find::name }, $dirphoto);

      Thank you - this worked perfectly for me.
Re: return an array from File::Find
by ww (Archbishop) on Mar 30, 2008 at 21:00 UTC

    After you guessed, did you check your syntax (perl -c scriptname.pl)?

    Unmatched right curly bracket at findfiles.pl line 5, at end of line syntax error at findfiles.pl line 5, near "}" findfiles.pl had compilation errors.

    So, adopting the answer from ForgotPasswordAgain above,

    #!/usr/bin/perl use strict; use warnings; use File::Find; my $dirphoto = "/home/ww"; my @array; find(sub { push @array, $File::Find::name }, $dirphoto); for (@array) { print "$_ \n";} print "done\n";

    should get the job done.

    Note also that using strict and warnings revealed several other issues with your guess; issues they would have helped you correct.

Re: return an array from File::Find
by FunkyMonk (Bishop) on Mar 30, 2008 at 21:05 UTC
    Or, you can forget File::Find and use the much superior (IMHO) File::Find::Rule:
    use File::Find::Rule; my @files = File::Find::Rule->file->in( "dir", "../another_dir" );

      Your code is (probably) not equivalent to what the OP wanted: his code (if it would have worked) will also return directories, while yours only returns proper files. Of course, the OP is not very clear about it: he speaks about files but his code does not check for just files.

      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