in reply to Diff between file and directory

Just a quick comment on your code. Always check to make sure opendir was successful:

opendir (DIR, $directory) || do { # your error handling here... };
-- vek --