adrianh gave me a really good solution, but I now have a new problem that puzzles me.
adrianh's idea was to overload CORE::GLOBAL::read(), but I am getting a strange behaviour - instead of returning to my calling code, it croaks directly.
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 }; # method that calls read() # the above causes that to fail, and so should # croak with a nice message $file->header(); }; # expected error message returned by the # croak in File::header() like($@, qr(Error in reading file header)); ...
And File::header() looks like this
package File; ... 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 is , $@ eq '').
So why aren't I croak()'ing ?
>perl -v This is perl, v5.8.3 built for i686-linux
use brain;
In reply to Overloaded *CORE:: function not behaving correctly by leriksen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |