For fun, I did a rewrite. I haven't tested it, but it compiles cleanly.

my $dir = "/Path/to/a/data/directory"; my %hash; $hash{$_} = hashify_file( $_ ) for ls_dir( $dir ); sub ls_dir { my $dir = shift; opendir my $dh, $dir or die "Can't opendir '$dir': $!\n"; return map { "$dir/$_" } grep { !/^\./ && !/\~$/ } readdir $dh; } sub hashify_file { my $file = shift; open my $fh, '<', $file or die "Can't read '$file': $!\n"; my $out; %{$out} = ( %{$out}, %{hashify_line( $_ )} ) for <$fh>; close $fh or die "Can't close '$file': $!\n"; return $out; } sub hashify_line { my $line = shift; chomp $line; $line =~ s/^\s+//; return {} if ( $line =~ /^\#/ || $line =~ /^\s*$/ ); my ( $key, @values ) = split /\t/, $line; return { $key => \@values }; }

Some things to be aware of...

As to your question, I think a grep on readdir would be the best way to go to get your list of files. The files themselves, you could process line-by-line instead of reading every line at once, and that might be better.

You might be able to get the shell to do even more of the work for you, though.

my $dir = "/Path/to/a/data/directory"; my %hash; open my $grep_fh, '-|', "grep '^' $dir/* /dev/null" or die "Can't grep: $!\n"; while ( my $line = <$grep_fh> ) { $line =~ s/^([^:])://; my $file = $1; next if $file =~ /\~$/; %{$hash{$file}} = (%{$hash{$file}}, %{hashify_line( $line )}); } close $grep_fh or die "Error closing grep pipe: $!\n";

This way, you get grep and the shell to do all the I/O.

Notes:

Update: broomduster makes a good point in Re: greater efficiency required (ls, glob, or readdir?) also. If you have too many files, the "$dir/*" to the shell is going to bomb. Time for xargs, then. Something like:

my $cmd = "find $dir -type f -print | xargs grep '^' /dev/null"; open my $grep_fh, '-|', $cmd or die "Can't grep: $!\n";

Then you may have to filter out dot files somewhere.


In reply to Re: greater efficiency required (ls, glob, or readdir?) by kyle
in thread greater efficiency required (ls, glob, or readdir?) by jperlq

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.