Re: Reading from standard input



Thanks John ! I finally understood the concept of '-' stream.

Harpreet

John W. Krahn wrote:
Harpreet wrote:

I am learning perl scripting and was reading an online tutorial where i
encountered this code(at the end of message). The first part of the
code (reading from file) has been pasted as-is and the second(reading
from standard input stream) was written by me. When I execute the
program, I get the correct result from the first one and then I type
into the stream some data but I don't know how to end it. I used the
conventional unix "dot-enter" scheme but it didn't work. Neither did
Ctrl-D. I found some examples which read from <STDIN> and worked with a
while loop. Can somebody explain me why reading from the standard input
doesn't work the way I have written ?

Because you are not reading from the standard input.

If I am missing the escape
character to denote the end of stream, please mention it.

Any pointers will be appreciated.


<CODE>
#!usr/local/bin/perl

You are using a relative path when you should be using an absolute path:

#!/usr/local/bin/perl

And don't forget:

use warnings;
use strict;


$file = '/etc/passwd'; # Name the file
open(INFO, $file); # Open the file

You should *ALWAYS* verify that the file opened correctly:

open(INFO, $file) or die "Cannot open '$file' $!";

@lines = <INFO>; # Read it into an array
close(INFO); # Close the file
print @lines; # Print the array

open(INFO, '-');

You should *ALWAYS* verify that the file opened correctly:

open(INFO, '-') or die "Cannot open '-' $!";

You are trying to open the file '-', standard input is not involved when
opening a plain file.

The *only* time that '-' has anything to do with standard input is when @ARGV
and <> are used:

local @ARGV = '-';

while ( <> ) { # read from STDIN because @ARGV contains '-'
print;
}

@lines2 = <INFO>; # Read it into an array
close(INFO);
print @lines2;
</CODE>



John
--
use Perl;
program
fulfillment

.



Relevant Pages

  • Re: Reading from standard input
    ... I am learning perl scripting and was reading an online tutorial where i ... into the stream some data but I don't know how to end it. ... Can somebody explain me why reading from the standard input ...
    (comp.lang.perl.misc)
  • Reading from standard input
    ... I am learning perl scripting and was reading an online tutorial where i ... into the stream some data but I don't know how to end it. ... Can somebody explain me why reading from the standard input ...
    (comp.lang.perl.misc)
  • Re: PowerCOBOL Hint. Hell, REVELATION!
    ... reading too many _dumps_?) ... gsar does accept 3 digit decimal ... standard input as binary rather than as a text stream is often a ...
    (comp.lang.cobol)
  • Re: Reading Simple Flatfiles with a COBOL Mindset
    ... I am new to C and old to COBOL ... > in with respect to reading a simple file - one record at a time. ... stream and impose your interpretation on them: ... there are text streams and binary streams. ...
    (comp.lang.c)
  • StreamReader.Read blocking?
    ... spawn threads that handle the reading of the streams by placing the stream ... Debug.WriteLine("Started reading standard output."); ... characters are read, or the end of the file is reached. ... About ReadBlock(), MSDN says: The method blocks until either count ...
    (microsoft.public.dotnet.framework)

Loading