FAQ 5.29 How can I tell whether there's a character waiting on a filehandle?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


5.29: How can I tell whether there's a character waiting on a filehandle?

    The very first thing you should do is look into getting the
    Term::ReadKey extension from CPAN. As we mentioned earlier, it now even
    has limited support for non-portable (read: not open systems, closed,
    proprietary, not POSIX, not Unix, etc) systems.

    You should also check out the Frequently Asked Questions list in
    comp.unix.* for things like this: the answer is essentially the same.
    It's very system dependent. Here's one solution that works on BSD

        sub key_ready {
            my($rin, $nfd);
            vec($rin, fileno(STDIN), 1) = 1;
            return $nfd = select($rin,undef,undef,0);

    If you want to find out how many characters are waiting, there's also
    the FIONREAD ioctl call to be looked at. The *h2ph* tool that comes with
    Perl tries to convert C include files to Perl code, which can be
    "require"d. FIONREAD ends up defined as a function in the *sys/ioctl.ph*

        require 'sys/ioctl.ph';

        $size = pack("L", 0);
        ioctl(FH, FIONREAD(), $size)    or die "Couldn't call ioctl: $!\n";
        $size = unpack("L", $size);

    If *h2ph* wasn't installed or doesn't work for you, you can *grep* the
    include files by hand:

        % grep FIONREAD /usr/include/*/*
        /usr/include/asm/ioctls.h:#define FIONREAD      0x541B

    Or write a small C program using the editor of champions:

        % cat > fionread.c
        #include <sys/ioctl.h>
        main() {
            printf("%#08x\n", FIONREAD);
        % cc -o fionread fionread.c
        % ./fionread

    And then hard code it, leaving porting as an exercise to your successor.

        $FIONREAD = 0x4004667f;         # XXX: opsys dependent

        $size = pack("L", 0);
        ioctl(FH, $FIONREAD, $size)     or die "Couldn't call ioctl: $!\n";
        $size = unpack("L", $size);

    FIONREAD requires a filehandle connected to a stream, meaning that
    sockets, pipes, and tty devices work, but *not* files.


Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Site Timeline