As the usual rant goes, you are not using
strict.pm,
your error checks on
open should include an explanation
of the error, the filename and $! (you only include $!),
and you should have a consistent indentation style.
While none relate to your specific question, all of them
will wind up helping you again and again.