in reply to How to erase the file content

Just open in '>' mode:
{ open my $fh, "> $file" or die "cannot open $file: $!" }