Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does die have to be used if opening a file fails?

Tags:

perl

Most of time, I do something like this:

open FH, ">file.txt" or die "Cann't open file: $!";

Does die have to be used? If I want my script to continue (and simply ignore the error if the file cannot be opened), what should I do?

like image 692
ericyoung Avatar asked Jun 25 '13 20:06

ericyoung


1 Answers

You might want to do something like

if(open my $fh, ">", "file.txt") {
    # do stuff with file
    close $fh;
}
else {
    # do stuff without file
} 
like image 156
AKHolland Avatar answered Dec 20 '22 23:12

AKHolland