in reply to $/ is playing havoc with my script.

Setting $/ to undef causes the next read on any file handle to be done in "slurp" mode -- i.e. everything from the current file offset pointer to the end of file gets pulled in as a single scalar value. Usually, this is done before the first read on a file handle, so the full content of the file comes in as a scalar on one read operation.

Setting $/ to something and then setting it back to its previous value, without doing any file input between those two settings, has no effect on anything -- so those two lines assigning to $/ within your while loop are pointless.

In scripts that use modules or that manage more than one input file, it is best to localize changes to $/ within the smallest essential code block, like this:

my $filecontents; if ( open( FILE, "whatever" )) { local $/; # this sets $/ locally to undef within this block $filecontents = <FILE>; close FILE; } else { die "whatever: $!"; } # all data from "whatever" is now contained in $filecontents # and $/ is now back to its previous value
There are other idioms involving "do { ... }", which would be easy to find here with SuperSearch.

(updated code snippet to fix missing paren)