Leave the hard stuff to autodie 

use autodie;

sub save_state {
    my($state) = @_;

    eval {
        open my $fh, '>', $state_file;
        print $fh $state;
        close($fh);
    };
    if($@) {
        die "Error saving state: $@";
    }
}