use Fatal qw(open close); sub save_state { my($new_state) = @_; eval { open my $fh, '>', $state_file; print $fh $new_state; close($fh); }; if($@) { die "Error saving state: $@"; } }