in reply to Regex negative lookahead

The negative lookahead used in your first case m/(?!\.pl$)/) says:

"The NEXT thing cannot be ".pl". When it sees text ending in ".pl", the thing AFTER the ".pl is NOT ".pl", it is the end of line. So, the match succeeds.

The problem is that it is a ZERO WIDTH assertion, and does not require any text to match.

I would write the expression as :

print "$file\n" if $file !~/\.pl$/
Which works fine.

If you want to avoid the "." and "..", you need to check for those :

next if $file =~/^\.\.?$|\.pl$/;

        What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
              -Larry Wall, 1992

Replies are listed 'Best First'.
Re^2: Regex negative lookahead
by aeqr (Novice) on Apr 25, 2014 at 04:35 UTC
    Thank you for your answer, I think I understand now. But about these "." and "..", are they files? Is "." the same "." as the current directory?
      Yes : "." is the current directory, and ".." is one level above.

      These are not files.

      It is traditional for them to be viewed in a directory listing.

              What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                    -Larry Wall, 1992

        On a Unix-y or POSIX-y system, directories are a special case of files, as are named pipes, device files, and a few other things. They are not regular files. Directories can be treated as files in some cases, but it is generally undesirable to do so. It's best to test.