I believe that the problem here is that you won't be
permitted to open a file for reading if it doesn't exist.
Try changing opening your output file to the following (>> for append and > for overwrite):
open (OUT, ">>$inputfile.out") or die "cannot open file: $!\n";