niko
niko

Reputation: 9393

Read multiple lines from console

How to read multiple lines from console in Perl?

I have used @a = <STDIN>; but I am unable to come out of that statement. Evertime I hit enter it goes to new line. I have read to hit ctrl+d to end the input but it does not seem to work.

Upvotes: 1

Views: 2742

Answers (3)

edi_allen
edi_allen

Reputation: 1872

It seems like you are on Windows. On Windows you have to hit Control-z on an empty line and then hit Enter.

Upvotes: 0

mpapec
mpapec

Reputation: 50657

You can use while loop,

my @a;
while (<STDIN>) {
    /\S/ or last; # last line if empty
    push @a, $_;
}

print @a;

Upvotes: 1

David W.
David W.

Reputation: 107070

Maybe a better idea would be a loop of some sort:

use strict;
use warnings;

my @a;
for(;;) {
    my $input = <STDIN>;
    last if not defined $input;
    chomp $input;
    push @a, $input;
}

This will end when you type in the Unix <EOF> (which is usually set to Ctrl-D by default).

Upvotes: 3

Related Questions