develooper Front page | perl.perl5.porters | Postings from April 2003

[perl #21260] adding error handling info to perlfunc/readline

Thread Next
From:
Casey West
Date:
April 24, 2003 14:24
Subject:
[perl #21260] adding error handling info to perlfunc/readline
Message ID:
20030424212904.GF34510@geeknest.com
This guy says that the readline docs don't talk about error messages,
he's correct.  Thanks to Chip for helping shape the example code, this
patch explains error handling for readline.

  Casey West

-- 
Shooting yourself in the foot with dBase 
You buy a gun. Bullets are only available from another company and are
promised to work so you buy them. Then you find out that the next
version of the gun is the one scheduled to actually shoot bullets. 

--- perl-current.orig/pod/perlfunc.pod  Mon Apr 14 16:47:02 2003
+++ perl-current/pod/perlfunc.pod       Thu Apr 24 17:20:12 2003
@@ -3832,7 +3832,21 @@
 operator is discussed in more detail in L<perlop/"I/O Operators">.
 
     $line = <STDIN>;
-    $line = readline(*STDIN);          # same thing
+    $line = readline(STDIN);           # same thing
+
+If readline encounters an error, C<$!> will be set with the corresponding
+error message.  It can be helpful to check C<$!> when you are reading from
+filehandles you don't trust, such as the tty or a socket.  The following
+example uses the operator form of C<readline>, and takes the necessary
+steps to ensure that C<readline> was successful.
+
+    for (;;) {
+        undef $!;
+        $line = <>;
+        die $! if $!;
+        last unless defined $line;
+        # ...
+    }
 
 =item readlink EXPR
 

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About