moderated >> How to test readline errors (or read errors for that matter)

by jrw32982 » Fri, 10 Nov 2006 04:13:47 GMT

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

moderated >> How to test readline errors (or read errors for that matter)

by John W. Krahn » Fri, 10 Nov 2006 06:54:33 GMT



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 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

moderated >> How to test readline errors (or read errors for that matter)

by jrw32982 » Sat, 11 Nov 2006 00:48:25 GMT


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?

moderated >> How to test readline errors (or read errors for that matter)

by John W. Krahn » Sat, 11 Nov 2006 05:05:46 GMT


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

1. Best way to handle readline errors?

Please enlighten.  What is the best-practice way to handle read errors
when using <> or readline?  What I'd really like is something like:

   while ($data = <$fh>) {
      # do something with $data
   }
   if ($fh->READLINE_ERROR) {   # but this method doesn't exist
      # handle readline error
   } else {
      # handle EOF
   }

but I don't think there is anything like this that will work in
reality.  Perldoc readline offers something like:

   while (1) {
      undef $!;   # doesn't really undefine $!
      $data = readline $fh;   # or $fh->readline
      if (!defined $data) {
         last unless $!;   # EOF
         # handle readline error
      }
      # do something with $data
   }

I believe this works properly, but it's kind of ugly.  Is this really
the best or only way?  I've found something like the following in
Stein's Network Programming with Perl:

   undef $!;
   while ($data = <$fh>) {
      # do something with $data
   }
   if (defined $!) {
      # handle readline error
   }

This is almost identical to my preferred way, but seems buggy because:
1) undef $! doesn't really undefine $! -- defined($!) returns true
after undef($!) !!
2) if the "do something" part sets $! then this will fail

The Camel 3rd edition doesn't seem to mention readline errors.  What
about using $fh->eof or $fh->error.  They don't seem to work quite like
I want to handle this case.  But they don't seem to be documented well
enough for me to tell for sure.

-- John Wiersba

2. xml read Error (while reading xml file with foriegn characters) - Perl

3. read() and IO:File->read() error

I have a file I am reading and when I get to a certain point read() and
IO::File->read() return undef.  I know I have not reached EOF.  Is there
anywhere I can find out why it can't read() or trap an error?

Thanks in advance
MS

4. "Invalid method in request" errors in Apache error log, was error - Perl

5. Answer to: test/ hyperlink to an Error in a column

Have a large document, want to find where error is asap & hyperlink to it?
also below, example of in-document link, to get you back to same spot, 
every-time.

ANSWER TO:  Find Error result in column, where calculated (pick a start 
point & exclude a range), Hyperlink to it (with row number as friendly name 
in hyperlink)

=HYPERLINK(IF(AND(
ISNA(INDEX(ROW($AX$173:$AX$540)-ROW(AX173)+1,MATCH(TRUE,EXACT($AX$173:$AX$540,"X"),0))),
ISNA(INDEX(ROW($AX$567:$AX$1168)-ROW(AX567)+1,MATCH(TRUE,EXACT($AX$567:$AX$1168,"X"),0)))),"",
"#"&CELL("address",OFFSET(INDIRECT(SUBSTITUTE(SUBSTITUTE(CELL("address",$W14),"$",""),ROW(),"")&INDEX(ROW($AX$173:$AX$1168),MATCH(TRUE,EXACT($AX$173:$AX$1168,"X"),0))),$W$1,0))),
IF($BX$15>0,IF(AND(
ISNA(INDEX(ROW($AX$173:$AX$540)-ROW(AX173)+1,MATCH(TRUE,EXACT($AX$173:$AX$540,"X"),0))),
ISNA(INDEX(ROW($AX$567:$AX$1168)-ROW(AX567)+1,MATCH(TRUE,EXACT($AX$567:$AX$1168,"X"),0)))),"",
INDEX(ROW($AX$173:$AX$1168),MATCH(TRUE,EXACT($AX$173:$AX$1168,"X"),0))),"-"))

note:  $W14 is the row that that formula resides in,  W is column you want 
cursor to end up in.

Quantity of Errors:
=IF($G$7="x",SUMPRODUCT(--(LEFT($AX$173:$AX$540,1)="x"))+SUMPRODUCT(--(LEFT($AX$567:$AX$1168,1)="x")),
SUMPRODUCT(--(LEFT($AX$173:$AX$540,1)="x"),--($G$173:$G$540<>"x")))+SUMPRODUCT(--(LEFT($AX$567:$AX$1168,1)="x"),--($G$567:$G$1168<>"x"))

SAMPLE:  Test for errors:
=IF(AND(S9<>".",OR($G$7="x",G9<>"x"),OR(CP9={"",0})),"X",

OTHER:
=HYPERLINK("#"&CELL("address",OFFSET($A$69,-1,1)),"top")

IN-DOCUMENT Hyperlink:  GETS YOU BACK TO EXACT SAME SPOT, EVERYTIME.
=HYPERLINK(IF(ROW($A$607)<(ROW($A543)-($W$1/2)),"#"&CELL("address",OFFSET($A$607,-1,1)),"#"&CELL("address",OFFSET($A$607,$W$1,1))),"A")

($A50 is row formula resides in)
Utility:  $W$1    method for screen size / from freezepane (any # minus size 
of freezepane = rows showing)
=48-CELL("row",$A$17)

6. Pb solved dmake.exe: Error code 255, while making 'test'

7. JPEG test error found w/ CPAN Upload: N/NI/NI-S/Tk-804.025_beta11.tar.gz

8. "make test" Error for the Tk version 804.027 (system RH9)