Global symbol "$fnames" requires explicit package

This specific error is because you have an array named @fnames but you later try to dereference an undeclared arrayref named $fnames.

You've also got an issue with the part of your syntax that tries to access the element within the array:

@$fnames->{filename}
That probably would not do what you think even if you didn't have the first problem. You can't access an element of an array, whether directly or via reference, by a key name.

If, as seems possible from the code you posted, you are receiving into your sub listdirs() two arguments, each a reference to an array, you can get what I think you want with:

my $res_files = list_dirs($dir,$datanames); sub list_dirs { my ( $locations, $fnames ) = @_; ... foreach my $href ( @{ $fnames } ) { my $filename = $href{'filename'}; # do something with filename ... } }
Or you could get the list of filenames with something like:
my $res_files = list_dirs($dir,$datanames); sub list_dirs { my ( $locations, $fnames ) = @_; my @filenames = map { $_->{'filename'} } @{ $fnames }; ... }
But, this raises the question of why you have an array of anonymous hashes containing just one key-value pair?

And in general, in Perl, there's usually a tool to do something common like what you want. You might like to use Path::Iterator::Rule:

use Path::Iterator::Rule; ... my $res_files = list_dirs($dir,$datanames); sub list_dirs { my ( $locations, $fnames ) = @_; # assumes each is a simple arrayref my $finder = Path::Iterator::Rule->new; $finder->name( @{ $fnames } ); my $get_next_match = $finder->iter( @{ $locations } ); my @found_filenames; while ( defined ( my $match = $get_next_match->() ) ) { push @found_filenames, $match; } return \@found_filenames; }

See also Yes, even you can use CPAN.

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: Passing file names to search directories by 1nickt
in thread Passing file names to search directories by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.