Reading from standard input

Discussion in 'Perl Misc' started by Harpreet, Sep 26, 2006.

  1. Harpreet

    Harpreet Guest

    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>
    Harpreet, Sep 26, 2006
    #1
    1. Advertising

  2. 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
    John W. Krahn, Sep 26, 2006
    #2
    1. Advertising

  3. Harpreet

    Ted Zlatanov Guest

    On 26 Sep 2006, wrote:

    > 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
    Ted Zlatanov, Sep 26, 2006
    #3
  4. Harpreet

    Harpreet Guest

    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
    Harpreet, Sep 27, 2006
    #4
  5. Michele Dondi wrote:
    > (I don't have John's post so I'm replying to you)


    That is because I cancelled it when I realised that I was wrong. :)


    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Sep 28, 2006
    #5
  6. Harpreet

    Harpreet Guest

    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 Dondi wrote:
    > (I don't have John's post so I'm replying to you)
    >
    > On 26 Sep 2006 21:40:18 -0700, "Harpreet" <>
    > wrote:
    >
    > >> 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:

    >
    > 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,
    Harpreet, Sep 29, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    365
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  2. funkyj
    Replies:
    5
    Views:
    1,097
    funkyj
    Jan 20, 2006
  3. MindClass

    Reading standard input

    MindClass, Oct 28, 2006, in forum: Python
    Replies:
    1
    Views:
    285
    Fredrik Lundh
    Oct 28, 2006
  4. sapsi
    Replies:
    6
    Views:
    501
    John Machin
    May 19, 2008
  5. Yuri Shtil

    Reading from piped standard input on w2k

    Yuri Shtil, Jun 30, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    98
    Tad McClellan
    Jun 30, 2003
Loading...

Share This Page