[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] IO:: problems (fwd)



mark@cheers.jsc.nasa.gov (Mark Manning/Muniz Eng.) writes
>According to Brian L. Matthews:
>>
>> With white space at the end, the open fails, so the print fails as
>> well. Both return an error, but because the little example doesn't
>> do any error checking, you don't get any error messages. It would be
>> completely and horribly wrong for either open or print to output
>> their own error messages instead of returning an error.
>
>I don't think it would be all that horrible.  Nor is it
>completely wrong.  I've done:
>
>        open( X, "y" ) || die "Couldn't open Y.";
>
>lots of times even though:
>
>        open( X, "y" ) || die $!;
>
>prints much better error messages.  Sometimes it's because
>of the large number of OPEN statements I'm using.  I've
>also found that always putting the filename is double
>quotes helps prevent "Not Found" statements.

I think this misses the point. It's not whether an error message gets
printed but whether that's done by open() or by the user's code. If it's
done by open then the programmer has no control over the message, nor is in
a position to handle the error in some other fashion. So in general library
routines should return error codes, which in the original code were being
ignored.

but I do agree you  always want to detect, and preferably report errors.

It's useful to know the name of the file being opened *and* the reason for
the problem. So I use a bare-bones version. In this case I'm quite happy
for my code to print an error message and quit, rather than attempt some
sort of error recovery. You wouldn't want commercial software quitting so
easily though.

sub  safe_open ($$)
{
    my ($FH, $fname) = @_;

    # it would be nice if the fatal message just listed
    # the basename, but it had a button for a longer message
    # which in this case would list the path
    open($FH, $fname) or fatal "trying to open '$fname': $!";
}


cheers,
Danny Thomas



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch