Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Input from Standard Input

by kevind0718 (Scribe)
on Feb 13, 2012 at 01:16 UTC ( [id://953372]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://953372]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-03-19 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found