title: Using Perl's readline , <> function with TCP socket and Signals
body: <p>I'm using Perl 5.8.8 and trying to determine if Perl automatically and consistently restarts the <a href="http://perldoc.perl.org/functions/readline.html" rel="nofollow noreferrer"><code>readline</code></a> function ( better known as <code>&lt;&gt;</code> ) if it's interrupted by a signal.  </p>
  
  <p>I want to safely read newline '\n' terminated strings from a TCP socket using readline.</p>
  
  <p>In the section <a href="http://perldoc.perl.org/perlipc.html#Deferred-Signals-(Safe-Signals)" rel="nofollow noreferrer">Deferred Signals (Safe Signals)</a> it says:</p>
  
  <blockquote>
    <p><strong>Restartable system calls</strong></p>
    
    <p>On systems that supported it, older
    versions of Perl used the <code>SA_RESTART</code>
    flag when installing <code>%SIG</code> handlers.
    This meant that restartable system
    calls would continue rather than
    returning when a signal arrived. In
    order to deliver deferred signals
    promptly, Perl 5.7.3 and later do not
    use <code>SA_RESTART</code>. Consequently,
    restartable system calls can fail
    (with <code>$!</code> set to <code>EINTR</code> ) in places
    where they previously would have
    succeeded.</p>
    
    <p>Note that the default :perlio layer
    will retry <code>read</code>, <code>write</code> and <code>close</code>
    as described above and that
    interrupted <code>wait</code> and <code>waitpid</code> calls
    will always be retried.</p>
  </blockquote>
  
  <p>Now it also says elsewhere that <code>readline</code> is implemented in terms of <code>read</code>.</p>
  
  <p>I'm thinking that if I do the following it should do what I want as I assume <code>readline</code> either returns a full line or <code>undef</code>:</p>
  
  <pre><code>sub Readline {
      my $sockfd = shift;
  
      my $line;
  
      while (!defined($line = readline($sockfd))) {
          next if $!{EINTR};
          last if eof($sockfd); # socket was closed
          die "readline: $!";
      }
      return $line;
  }
  </code></pre>
  
  <p>Will this do what I want?</p>