|I suspect that this is for a homework assignment, so I won't provide the complete solution, but I will guide you.|
First, every Perl script you write should include the strict and warnings pragmas which will point out lots of mistakes that can be difficult to troubleshoot. So, add these two lines near the top of the script.
The strict pragma forces you to declare your vars. So to declare $TARGET as a lexical var, you'd use the my keyword.
my $TARGET = "targethostnames.txt";
Now, also due to the strict pragma, the open call you have will no longer work as written. You need to specify the filename.
See: perldoc -f open
Now we're getting to the cause of your problem, which is 2 fold and I'm finding it hard to guide you here without providing the solution.
You never (or maybe I should say almost never) want to use a foreach loop to process the data coming from a filehandle. Instead, you should use a "while loop".
Now, each line in the file has a line terminator which needs to be stripped off. That is done with the chomp function.