You probably figured out the syntax error by now, but in case you didn't: the first "foreach" loop (over "my @dir (@directories)") does not have an open curly brace. Fix that, and make sure the other braces are all properly balanced. This is a lot easier if you use a text editor that knows about programming languages (C-style indentation is good enough, but the really good programming editors, which provide syntax highlighting as well as automatic indentation, have a mode for editing Perl.)
So long as you understand and trust the editor's automatic suggestions about how far indentation should go for a given line, it'll help you to know when and where to look for various brackets, braces, parens and quotes that happen to be missing or misplaced.