chomp(my @checked_names = do { open my $fh, "<", $config_file ? (<$fh>) : $! =~ /no such file/i ? () : die "Can't open $config_file to read: $!"; }); # ... do stuff with @checked_names open my $fh, ">", $config_file or die "Can't open $config_file to writ +e: $!"; print $fh map "$_\n", @checked_names; close $fh;
Note the use of a lexical to hold the filehandle in the do block. This means the file will be auto-closed when the $fh goes out of scope at the end of the block: otherwise, we'd have to follow the ternary with a close, forcing us to store the file content in a temporary array to be able to return it.
Although from the looks of this, since the file is reopened for writing later anyway, there may not be much incentive to die on the read attempt even if the error was due to something other than a nonexistant file. YMMV
Update: merlyn is right, of course.
Makeshifts last the longest.
In reply to Re: To die or not to die
by Aristotle
in thread To die or not to die
by Popcorn Dave
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |