On 7/12/06, Mr. Shawn H. Corey <[EMAIL PROTECTED]> wrote:
Jay Savage wrote:
>   opendir(DIR, $somedir) or die "$!\n";
>   my @files = grep { $_ !~ /^\.{1,2}$/ && -f "$somedir/$_" } readdir(DIR);
>   closedir(DIR);

my @files = grep { -f } glob( "$somedir/*" );

If you're not interested in subdirectories, you only need glob(). And
yes, it doesn't list hidden files; that's because it shouldn't. Hidden
files are HIDDEN!



Now you're just being silly. By that logic, ls shouldn't have an -a
flag. There are times you want ls, and times you want ls -a. readdir
lets you make your own decisions, glob takes the DWIM appraoch, both
have thier uses. In most cases, File::Find will be better than either.
But that's been suggested several times. OP had a question about
reddir, and I answered with with an answer about readdir.

The problem with glob here is that it makes some assumptions about the
input, which are almost guaranteed to be incorrect. It attempts to
mimic unix shell conventions. It doesn't skip hidden files. It skips
files that would be hidden under sh and it's descendants. OP, however,
is working with DOS directory listings. '.file', for instance, is a
perfectly valid, visible DOS file name, but glob will ignore it.

  my @files = grep { $_ !~ /^\.{1,2}$/ && -f "$somedir/$_" } readdir(DIR);

On the other hand, leaves valid file names intact and only removes '.'
and '..' from the list.

If we then need to move another level deeper, we can.

Or just use File::Find.

Best,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to