in reply to write to textfile

Include the $! variable in you die statements. It may make you wiser:
... die "Shuffling off mortal coil: $!\n";


[ar0n]