How to test readline errors (or read errors for that matter) - Perl
This is a discussion on How to test readline errors (or read errors for that matter) - Perl ; Any suggestions for how to create a test which checks if my readline
error handling is working properly? How can I get readline (or read
for that matter) to fail after open succeeds?
-- John Wiersba...
-
How to test readline errors (or read errors for that matter)
Any suggestions for how to create a test which checks if my readline
error handling is working properly? How can I get readline (or read
for that matter) to fail after open succeeds?
-- John Wiersba
-
Re: How to test readline errors (or read errors for that matter)
jrw32982@yahoo.com wrote:
> Any suggestions for how to create a test which checks if my readline
> error handling is working properly? How can I get readline (or read
> for that matter) to fail after open succeeds?
perldoc -f readline
[ snip ]
If readline encounters an operating system error, $! will be set
with the corresponding error message. It can be helpful to check $!
when you are reading from filehandles you don’t trust, such as a tty
or a socket. The following example uses the operator form of
"readline", and takes the necessary steps to ensure that "readline"
was successful.
for (;
{
undef $!;
unless (defined( $line = <> )) {
die $! if $!;
last; # reached EOF
}
# ...
}
As for read(), it returns undef on error so:
defined( read FILEHANDLE, $buffer, $size ) or die "read error: $!";
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
-
Re: How to test readline errors (or read errors for that matter)
John W. Krahn wrote:
> ...snip...
I've seen the doc -- but how can I create a test which actually
generates a read or readline error so that I can test if my error
handling works properly?
-
Re: How to test readline errors (or read errors for that matter)
jrw32982@yahoo.com wrote:
> John W. Krahn wrote:
>>...snip...
>
> I've seen the doc -- but how can I create a test which actually
> generates a read or readline error so that I can test if my error
> handling works properly?
According to the read(2) man page:
ERRORS
EAGAIN Non-blocking I/O has been selected using O_NONBLOCK and no data
was immediately available for reading.
EBADF fd is not a valid file descriptor or is not open for reading.
EFAULT buf is outside your accessible address space.
EINTR The call was interrupted by a signal before any data was read.
EINVAL fd is attached to an object which is unsuitable for reading.
EIO I/O error. This will happen for example when the process is in a
background process group, tries to read from its controlling
tty, and either it is ignoring or blocking SIGTTIN or its
process group is orphaned. It may also occur when there is a
low-level I/O error while reading from a disk or tape.
EISDIR fd refers to a directory.
Other errors may occur, depending on the object connected to fd.
POSIX allows a read that is interrupted after reading some data to
return -1 (with errno set to EINTR) or to return the number of bytes
already read.
So try one of those.
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Similar Threads
-
By Application Development in forum Software-Testing
Replies: 1
Last Post: 01-08-2007, 01:24 PM
-
By Application Development in forum Perl
Replies: 0
Last Post: 11-09-2006, 03:11 PM
-
By Application Development in forum Java
Replies: 4
Last Post: 11-06-2006, 01:28 PM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 06-20-2006, 10:58 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 06-07-2006, 06:48 AM