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

Hello Kind Monks:

Working my way through Learning Perl (The Llama).

In chapter 5, Input and Output there is this sample:
foreach(<STDIN>) { print "I saw $_"; }
Then the text talks about how this foreach, because it is in list context. Will load the full <STDIN>.

My question is:

Where is the input stored? In @_ I do not think so.

How can I get the length of the input? Maybe I had read in a large file and I only wanted to process the last 50 lines.

As always thanks for your kind assistance.

KD

Replies are listed 'Best First'.
Re: Input from Standard Input
by choroba (Cardinal) on Feb 13, 2012 at 01:40 UTC
    Using this particular syntax, the input is not accessible. You can assign it to an array if you want to do something with it:
    my @input = <STDIN>; print 'Size: ', scalar(@input), " lines.\n";
    Update: The last 50 lines are then accessible in
    @input[-50 .. -1]
Re: Input from Standard Input
by Anonymous Monk on Feb 13, 2012 at 01:42 UTC

    Where is the input stored? In @_ I do not think so.

    It is stored on the stack, in other words, there is no variable like @_, which you can use

    You can however make one

    my @stuff = <STDIN>; my $thismany = @stuff; print "I saw this many ", scalar(@stuff), "\n"; print "I saw this many ", int(@stuff), "\n"; print "I saw this many ", 0+@stuff, "\n"; print "I saw this many $thismany\n"; foreach(@stuff){ print "I saw $_"; }
      yep that all makes sence. thanks KD