I'm going to follow up a second time here...

I was thinking a little more about your question. I remember back to the days of my Econ classes, where the professor could answer just about any question with "It depends." How are you reading STDIN, and where is it getting its stream from? Let's say, for example, you've got a bunch of files listed on the command line, and you're reading them via the empty diamond operator: <> (see perlop). Before you start reading from them, @ARGV is going to have a list of filenames that were present on the command line when the script was invoked. It would be easy enough to use the -s function (perldoc -f -X) to check the file size.

my %sizes; die "Can't pre-measure a TTY stream\n" if -t; foreach( @ARGV ) { if( my $size == -s ) { $sizes{ $_ } = $size; } }

The -t test checks whether your input is coming from a terminal. If it is, as BrowserUk pointed out, you can't measure it, just as you can't tell me today what date I should mark in my calendar as the end of time as we know it.

However, even -t is not getting you the whole story. Probably the best thing to do would be to fail unless -f (unless you're looking at a file). That's because your stream could be coming from a socket or a named pipe, and rather than test each of those cases individually, you may as well just fail if you're not looking at a file.


Dave


In reply to Re: Counting the size of STDIN by davido
in thread Counting the size of STDIN by mikealeonetti

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.