Reading from standard input

H

Harpreet

Hi.

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 ? If I am missing the escape
character to denote the end of stream, please mention it.

Any pointers will be appreciated.

Thanks and Regards,

Harpreet.


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

$file = '/etc/passwd'; # Name the file
open(INFO, $file); # Open the file
@lines = <INFO>; # Read it into an array
close(INFO); # Close the file
print @lines; # Print the array

open(INFO, '-');
@lines2 = <INFO>; # Read it into an array
close(INFO);
print @lines2;
</CODE>
 
J

John W. Krahn

Harpreet said:
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
 
T

Ted Zlatanov

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 ? If I am missing the escape character to denote the end of
stream, please mention it.

You may be on a Windows or DOS system, where Ctrl-Z is the end of file
character, not Ctrl-D as in other systems. Try that.

Ted
 
H

Harpreet

Hi Michele,

I didn't understand what you said earlier but I experimented with the
code you had posted and it works exactly the way I wanted. But since I
have just started exploring perl, things like Mfatal and Mdata don't
make much sense to me. I hope to read all that in due time and
understand what you said. Do you have a good soft document or pointer
to an online resource through which I can learn perl.

regards,

Harpreet


Michele said:
(I don't have John's post so I'm replying to you)


I thought so too. But that is *not* the case, and the docs confirm
that:

: In the 2-arguments (and 1-argument) form opening '-' opens STDIN
: and opening '>-' opens STDOUT.

Yet, not a particularly good reason to use it, especially when you
already have STDIN.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top