I've wondered the same thing. First, for filehandles, I know you can use -s HANDLE to return the size of the file. If you use the read to read from the file, it will return the number of bytes read, and you can subtract that from another variable you initially set to the file size. Unfortunately, this (as far as I know) only works for files.

I've tried something similar on sockets and -s just doesn't return anything unless there's data. If there isn't any, it waits until there is, which could be never. So, to conditionally read from the socket if there was any data on it, I installed an SIGALRM handler to timeout any read operations after 1 second. Of course if I didn't want it to pause that would just totally suck. Anyway, I'm rambling, but I know of no way to check how much data is waiting on a socket. Still, read does return how much data was read from a socket, but someone please correct me if I'm wrong, it will continue to read, or try to read, until it reads either an EOF or however many bytes you tell it to read. Plus you have no way of knowing how many bytes are waiting to be read in the first place, and thus no way to know how much is left.

I'm going to vote up your node because I really really want an answer to this myself, and for some reason it hasn't landed on the SOPW page yet.

72656B636148206C72655020726568746F6E41207473754A


In reply to Re: Deteriming how much is left to read on a filehandle by reptile
in thread Deteriming how much is left to read on a filehandle by strredwolf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.