http://qs321.pair.com?node_id=1173661

perldigious has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Monasterians,

I'm asking for opinions on possibly better ways to do the following.

open(my $Raw_fh, ">", "Output_Raw.txt") or file_open_error_abort("Outp +ut_Raw.txt"); # This subroutine will print a file open error message, including the +filename passed to it, and terminate the program after an acknowledge +ment from the user. sub file_open_error_abort { my $filename = $_[0]; say "Cannot open \"$filename\": $!."; print "---Press Enter to quit---"; my $pause = <STDIN>; exit; }

I'm really just forcing the script to pause so that the user can see the error message. When I just used the standard file open() or die pragma I would get frequent calls from people attempting to manually run scripts I'd written saying they weren't working (because the window would just close immediately), and the vast majority of the time it would simply be because they had an output file open in another program therefore denying permission for the script to write to it. The my $pause = <STDIN>; pause so they can read the message largely fixed this annoyance for me, but every time I use it I cringe a little and wonder if there isn't a better, perhaps more perlish way of doing this.

UPDATE BELOW:

Thanks davido and tye, I'm going to take parts of both of your suggestions and go with the following.

use utf8; use 5.022; use strict; say "BEGIN"; open(my $out_fh, ">", "permission_denied_output_file.txt") or die "Can +not open \"permission_denied_output_file.txt\": $!."; open(my $in_fh, "<", "no_such_input_file.txt") or die "Cannot open \"n +o_such_input_file.txt\": $!."; say "END"; END { use ExtUtils::MakeMaker qw(prompt); # placing this use statement h +ere for convenient copy-paste of this END block code to other scripts +. prompt('Please hit enter to exit.') if ($?); # stop and prompt for + acknowledgement if program exits with a non-zero value. }

This code doesn't give me the same cringe as my kludge function did. I tested it with an .exe generated via PAR::PACKER and it works as expected for the two cases above (on my machine anyway) and doesn't cause any pause if there are no file open dies.

Much appreciated, as always.

I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious