<p>I'm writing my first Perl app -- an AOL Instant Messenger bot that talks to an Arduino microcontroller, which in turn controls a servo that will push the power button on our sysadmin's server, which freezes randomly every 28 hours or so. </p>

<p>I've gotten all the hard stuff done, I'm just trying to add one last bit of code to break the main loop and log out of AIM when the user types 'quit'.</p>

<p>The problem is, if I try to read from STDIN in the main program loop, it blocks the process until input is entered, essentially rendering the bot inactive. I've tried testing for EOF before reading, but no dice... EOF just always returns false.</p>

<p>Here's below is some sample code I'm working with:</p>

<pre><code>while(1) {
    $oscar-&gt;do_one_loop();

# Poll to see if any arduino data is coming in over serial port
    my $char = $port-&gt;lookfor();

# If we get data from arduino, then print it
    if ($char) {
        print "" . $char ;
    }

    # reading STDIN blocks until input is received... AAARG!
    my $a = &lt;STDIN&gt;;
    print $a;
    if($a eq "exit" || $a eq "quit" || $a eq 'c' || $a eq 'q') {last;}
}

print "Signing off... ";

$oscar-&gt;signoff();
print "Done\n";
print "Closing serial port... ";
$port-&gt;close() || warn "close failed";
print "Done\n";
</code></pre>

[{'answer_id': 14124, 'author': 'jj33', 'author_id': 430, 'author_profile': 'https://Stackoverflow.com/users/430', 'pm_score': 5, 'selected': True, 'text': '<p>The Perl built-in is <a href="http://perldoc.perl.org/functions/select.html" rel="noreferrer"><code>select()</code></a>, which is a pass-through to the <code>select()</code> system call, but for sane people I recommend <a href="http://search.cpan.org/dist/IO/lib/IO/Select.pm" rel="noreferrer"><code>IO::Select</code></a>.</p>\n\n<p>Code sample:</p>\n\n<pre><code>#!/usr/bin/perl\n\nuse IO::Select;\n\n$s = IO::Select-&gt;new();\n$s-&gt;add(\\*STDIN);\n\nwhile (++$i) {\n  print "Hiya $i!\\n";\n  sleep(5);\n  if ($s-&gt;can_read(.5)) {\n    chomp($foo = &lt;STDIN&gt;);\n    print "Got \'$foo\' from STDIN\\n";\n  }\n}\n</code></pre>\n'}, {'answer_id': 54816600, 'author': 'Gus Schlachter', 'author_id': 3935928, 'author_profile': 'https://Stackoverflow.com/users/3935928', 'pm_score': 2, 'selected': False, 'text': '<p>I found that <a href="http://search.cpan.org/dist/IO/lib/IO/Select.pm" rel="nofollow noreferrer">IO::Select</a> works fine as long as STDOUT gets closed, such as when the upstream process in the pipeline exits, or input is from a file.  However, if output is ongoing (such as from "tail -f") then any partial data buffered by <code>&lt;STDIN&gt;</code> will not be displayed.  Instead, use the unbuffered <a href="https://perldoc.perl.org/functions/sysread.html" rel="nofollow noreferrer">sysread</a>:</p>\n\n<pre><code>#!/usr/bin/perl\nuse IO::Select;\n$s = IO::Select-&gt;new(\\*STDIN);\n\nwhile (++$i) {\n        if ($s-&gt;can_read(2)) {\n                last unless defined($foo=get_unbuf_line());\n                print "Got \'$foo\'\\n";\n        }\n}\n\nsub get_unbuf_line {\n        my $line="";\n        while (sysread(STDIN, my $nextbyte, 1)) {\n                return $line if $nextbyte eq "\\n";\n                $line .= $nextbyte;\n        }\n        return(undef);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/14118', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']