in reply to special variable?

Welcome to the Monastery!

I think instead of $' (which is read-only), you want $", which is what gets put between list elements interpolated into a double quoted string.

As an aside, I'd suggest that this:

open (DAT, ">>$filename") || die ("no file exists");

...be more like this:

open my $dat, '>>', $filename or die "Can't append '$filename': $!";

That way you get a better error message, and it won't matter if $filename starts with some strange characters. I'm opening a lexical scalar as a filehandle here too, so you'd use "$dat" everywhere you had "DAT" before.

Updated to add that I also agree with toolic that using a special variable here is not really necessary.