in reply to special directory entries

One thing to watch out for though, the regex /^\.\.$/ wont do the right thing with a file named ".\n" or "..\n".
#!/usr/bin/perl -wT use strict; my @arr = (".", "..", ".\n", "..\n", "file.txt"); my @files = grep !/^\.\.?$/, @arr; print "'$_'\n" for @files; # <== only prints 'file.txt'
A better regex would be /^\.\.?\z/ since it will deal with these odd filenames correctly.

Update: Reworded the first sentence, since it was rather unclear.

-Blake

Replies are listed 'Best First'.
Re: Re: special directory entries
by Fastolfe (Vicar) on Dec 04, 2001 at 02:29 UTC

    (Update: I guess I missed the last line of your comment, and for some reason interpreted your comment to mean exactly the opposite of what you were trying to say, so disregard this note! Sorry.)

    Under what conditions would a file named "..\n" exist, and why would you necessarily want to omit it? If you're looking to get at all files in a directory, but skip the special . and .., and you still want to get files that someone's done crazy stuff with, you wouldn't necessarily want to omit these.

    Or am I misunderstanding something?

      If a malicious hacker creates a file (or directory) called ".\n" a monitoring script using /^\.\.$/ will lump it in with '.' and '..' and skip right over it.

      -Blake