jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,
When I go like dis:
my @a=<STDIN>; print @a;

I lose the first line of the <STDIN> and get all the rest.
But, if I add a newline to each print like dis:
my @a=<STDIN>; print "\n @a";

Then I get all of the lines of the <STDIN>.
And finally if I
my @a=<STDIN>; chomp(@a); print @a;

I get nothin at all! Why?
TIA
jg

Replies are listed 'Best First'.
Re: Line input clipped from my array?
by Zaxo (Archbishop) on Sep 24, 2001 at 09:14 UTC

    I think you need to show more of the code. Here is what I get with your three examples:

    $ echo "foo > bar > baz > " | perl -e 'my @a = <STDIN>; print @a' foo bar baz $ echo "foo > bar > baz > " | perl -e 'my @a = <STDIN>; print "\n @a"' foo bar baz $ echo "foo > bar > baz > " | perl -e 'my @a = <STDIN>; chomp(@a); print @a' foobarbaz$
    Do you do other manipulations of STDIN?

    Update: Hmmmm, that is strange. I think chromatic's suggestion of something funny in the input file is likely. Also, do you (maybe in some init file) set $[=1; to get one-based indexing? That doesn't seem likely, but this is a puzzler.

    After Compline,
    Zaxo

      That is exactly what is bugging me about this Zaxo, that IS all the code:
      #!/usr/local/bin/perl -w use strict; my @a=<STDIN>; print @a;
      I am so baffled! I'm doing this as one of the exercises in Llama Ch. 4. I had assumed y'all would respond with a "Well of course it's doing that, you did ......."
      jg
        Additionally,
        #!/usr/local/bin/perl -w use strict; my @a=<STDIN>; print $a[0]; print $a[1]; print $a[2];

        returns the contents of  $a[1] and  $a[2] but nothing (not even a newline) for  $a[0] .
        Arghhhh!
        jg
Re: Line input clipped from my array?
by John M. Dlugosz (Monsignor) on Sep 24, 2001 at 09:11 UTC
    In all three cases, you are not seeing whatever material comes before the first \n of output.

    In the middle case, you add a \n before the first string, working around it. In the third case, you have no \n's at all, so you lose it all.

    Why your terminal is behaving that way is something that's beyond the information you presented. I suggest you explore the "first \n" idea in your context to get to the bottom of it, and use your first case above in a stand-alone program run in the normal way, to verify that it does work.

    —John

Re: Line input clipped from my array?
by chromatic (Archbishop) on Sep 25, 2001 at 00:14 UTC
    Perhaps you have a carriage return on the first line of your file.
    my @a = map { tr/\r//d; $_ } <STDIN>; print @a;
Problem Identified
by jerrygarciuh (Curate) on Sep 25, 2001 at 23:02 UTC
    Apparently if you are running ActivePerl on Win98 there is a known issue with CTRL-Z. The first run of the script is reported to run fine although I am unsure if that is always true. Thereafter, at least for that session, all input up to the first newline is lost. There may be workarounds for this, but as I am only doing Llama tutes right now I have chosen to ignore it and focus on learning the lessons.
    Peace,
    jg