in reply to Delete last line of file with regex

If it's not a huge file, I'd use File::Slurp (or maybe File::Slurper if the issues it fixes are relevant for your case).

@lines = read_file($file); pop @lines if $lines[-1] =~ /^##/; write_file($file, @lines);