in reply to Finding item in a text file

Apart from figuring out what your intention is, you do seem to be having a problem with the use of file operations. These lines in your code are contradictory:
open(DATA_IN, ">>$template_data") || print ... ... $_ = <DATA_IN>; # No, you can't do this on an output file ...

If you want to read an existing file, make some changes to the contents, and write the modified content back to the original file, there are a few ways to do this. The "best" way may depend on how big the file is, and what sorts of changes you need to make to the contents. If the files are not huge (i.e. not multi-megabyte), and the changes might span multiple lines of text, then here's a good way:

my $filename="test.txt"; my $template_data = "unsecure/".$filename; $/ = undef; open( DATA_IN, $template_data ) or die "$!\n"; $_ = <DATA_IN>; # read the whole file into $_; close DATA_IN; # now check for patterns in $_ that need to change, # and change them. I'm not clear yet on this part # of the problem... but when this is done, finish with: open( DATA_OUT, ">$template_data.new" ) or die "$!\n"; print DATA_OUT; close DATA_OUT; __END__

Note that I used a different name for the output file -- this would be a good strategy for you until you have made sure that the central part of the script is doing what you want, and not corrupting the data. (Of course, you could have it over-write the input file, and just make sure you keep a separate copy of the original input, so that it will be easy to replace the input with a fresh copy each time you try to debug the script.)