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

I am attempting to open a file (or do some other operation), and if it can't be opened (or completed) for some reason, I need to print a message and then force the user to hit <ENTER> before the program exits. I have the following code:

$file="c:\\file.txt"; open(FILE,"<$file") || do { {local( $| )= 1; print "Error xxx\n"; print "Hit <ENTER> to exit.\n"; my $resp= <STDIN>; }# end local exit; }# end do @array=<FILE>; close(FILE) || die "close0: $!\n";
It works if I comment out the last two lines, however with them in I get an error? Ideas?

Replies are listed 'Best First'.
Re: Problem with do statement
by Joost (Canon) on Apr 18, 2005 at 14:27 UTC
Re: Problem with do statement
by Zaxo (Archbishop) on Apr 18, 2005 at 14:30 UTC

    You need a semicolon after the end of the do block to finish the statement.

    I'm curious why you want to do that. The standard open my $fh, '<', $file or die 'Error xxx'; would have the same effect, but could run unattended.

    After Compline,
    Zaxo

      To throw a random guess, I would say he might be running the program by double-clicking on an icon from something like Windows Explorer. It opens the script in a terminal window, and executes, but then closes as soon as the program finishes. If there was an error message displayed, it would disappear nearly instantly.

Re: Problem with do statement
by Fletch (Bishop) on Apr 18, 2005 at 14:34 UTC

    Missing semicolons aside, it might read more clearly as:

    my $file = "..."; unless( open( FILE, "<", $file ) ) { print STDERR "Error opening '$file': $!\n" print STDERR "Hit <ENTER> to exit\n"; scalar <STDIN>; exit 1; } my @array = <FILE>; close( FILE ) or die "close '$file': $!\n";