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

In reply to Getting the Behavior of the "file open or die" Pragma but with a Program Pause by perldigious

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.