in reply to Compilation Error.

Perhaps augmenting marto's on-point observation, please note:
      opendir takes a directory name, not a filename. But with $0, what you get is full\path\to\filename.

Using strict and warnings is a good idea -- in fact, damn near mandatory unless you have a very specific and knowledgeable reason to omit either or both. They're there to help, and in this case would have advised you "Invalid argument at (your opendir or readdir lines)."