#!usr/bin/perl
use strict ;
use warnings ;
sub print_ele_arr {
my @arr;
while (@arr < 3) {
push @arr, scalar <STDIN>;
}
# chomp(@arr); # remove newlines from @arr elements?
print "@arr\n";
}
print_ele_arr() ;
When reading from STDIN
or any other input file handle there are two ways to it, doing it in scalar or list context.
List context forces reading all lines at once, and scalar reads one line at the time. Since STDIN
doesn't have fixed size, it's better to force scalar context, using scalar
function.
This is necessary when populating array, and redundant when populating plain scalar, ie.
my $single_line = <STDIN>;
is same thing as writing
my $single_line = scalar <STDIN>;