in reply to Counting the size of STDIN

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

Replies are listed 'Best First'.
Re^2: Counting the size of STDIN
by tchrist (Pilgrim) on Apr 27, 2011 at 05:31 UTC
    You also can’t size sockets, pipes, and a whole bunch of devices.
Re^2: Counting the size of STDIN
by mikealeonetti (Novice) on Apr 27, 2011 at 13:38 UTC
    It's going to be data piped in from the terminal. I guess I will have to count using length. It's not the end of the world. I was just checking :D.