in reply to To die or not to die

I'll assume you use an array to store the defaults. Then I'd say something like:
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.

Replies are listed 'Best First'.
•Re: Re: To die or not to die
by merlyn (Sage) on Oct 29, 2002 at 21:09 UTC
    Note the use of a lexical to hold the filehandle in the do block.
    So noted.

    But I don't see "require 5.006;" at the top of your code, because you just made it non-portable for 5.005 sites.

    I hope you're aware of that. If not, you are now. And so is anyone else reading this thread.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.