Re: Reading from standard input
- From: "Harpreet" <harpreet.saluja@xxxxxxxxx>
- Date: 26 Sep 2006 21:40:18 -0700
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
.
- References:
- Reading from standard input
- From: Harpreet
- Re: Reading from standard input
- From: John W. Krahn
- Reading from standard input
- Prev by Date: Re: remove last 10 lines of all files in a directory
- Next by Date: Re: Need help with pattern matching/substitution.
- Previous by thread: Re: Reading from standard input
- Next by thread: Re: Reading from standard input
- Index(es):
Relevant Pages
|
Loading