in reply to Opendir error

I think you want something along the following lines:

use strict; opendir(DIR,".") or die("Can't open directory '$!'"); while (my $file = readdir(DIR)) { next if $file =~ /^\.{1,2}$/; print "$file\n"; } closedir(DIR);
A dir handle can only be read by readdir, not via the diamond <>. You probably want to exclude the . directory, so you have to escape the . in the regex and most probably you'd also prefer not to have .. either.

Hope this helps, -gjb-

Replies are listed 'Best First'.
Re:x2 Opendir error (don't filter with a regex)
by grinder (Bishop) on Jan 17, 2003 at 14:24 UTC

    Don't promote the /^\.{1,2}$/ meme. It doesn't work against malicious people who create files named ..\nfoo. For that you have to use \z instead of $, and by then it is starting to become quite unreadable.

    It is much better to write

    next if $file eq '.' or $file eq '..';

    Of course, a pendant might also go as far as saying

    next if $file eq File::Spec->curdir or $file eq File::Spec->updir;

    Personally I don't bother with that, but it does make for nice cross-platform code. See special directory entries for another thread on the question.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
      Yes. And depending on the processing job, I would encourage the use of !/^\./ whenever possible. Filenames beginning with a dot are supposed to be "hidden", so unless you need to process "hidden" files as well, you should ignore all names that start with a dot.

      jdporter
      The 6th Rule of Perl Club is -- There is no Rule #6.