in reply to Re: Testing for read() failures on different platforms
in thread Testing for read() failures on different platforms

What on earth would generate a read failure?

Networked file systems, corrupt media, broken pipe, etc. Not checking for read errors can lead to evil bugs :-)

As to the OPs question:

... can anyone advise me on a better way for testing cross platform read failures?

I find redefining read is the simplest solution. At its most basic just adding

BEGIN { *CORE::GLOBAL::read = sub (*\$$;$) { return undef }; };

to your test file will always cause read to fail.

Replies are listed 'Best First'.
Re^3: Testing for read() failures on different platforms
by leriksen (Curate) on Jul 28, 2004 at 01:06 UTC
    Yep, of course.
    And nicely extensible to other functions in the CORE package I imagine, like testing failing write()'s, etc.

    That is the solution I will use.

    Thank you

    +++++++++++++++++
    #!/usr/bin/perl
    use warnings;use strict;use brain;

Re^3: Testing for read() failures on different platforms
by leriksen (Curate) on Jul 28, 2004 at 07:13 UTC
    Bugger, that doesn't work, and it behaves funny too.

    Here's the code in the File.t file

    ... $file = File->new('t/file7.test'); eval { # declare as local so default is restored on block exit local *CORE::GLOBAL::read = sub (*\$$;$) { return undef }; $file->header(); }; ...

    And File::header() looks like this

    ... sub header { my ( $self ) = @_; my $seek_rc = $self->{FH}->seek($self->{Header}->{OFFSET}, SEEK_SE +T ); if ($seek_rc == 0) { croak( 'Cannot seek to absolute file position on opened file handl +e - ', $! ); } my $length = $self->{FH}->read(my $header, $self->{Header}->{Lengt +h}); # check if read() had an error croak( "Error in reading file header - $!") # pathological test unless defined $length; croak( 'Error in reading file header - Mismatch between expected l +ength (', $self->{Header}->{Length}, ") and returned length ($length +) byte count") unless $length == $self->{Header}->{_Length}; $header =~ s/(?:\015+)?\012.*$/\n/; # remove any garbage at end +of header and replace with \n return $header; } ...

    So if the read() returns undef, I expect the croak() message to appear with the $! reason for failure.

    Instead, I don't get my croak() message , all I get is $! populated with 'Bad file descriptor'. $@ is empty (that us , $@ eq '').

    Whatsmore, this happens on Linux. Both $@ and $! are still an empty string on Solaris.

    So why aren't I croak()'ing ?

    use brain;