From: Ben Morrow on

Quoth Mart van de Wege <mvdwege(a)mail.com>:
> Why it is necessary here is because the above statement would normally
> evaluate <> in list context. I'm a bit unclear if that is because of the
> array assignment or the map function. I believe it is the map function
> forcing a list context here, but I could be wrong. Even if I am,
> assigning to an array definitely forces list context.

map always evaluates its first argument (EXPR or BLOCK) in list context.
This makes it possible to change the length of the returned list.

Ben

From: Dr.Ruud on
Peng Yu wrote:

> I want to give a given number of lines. Current, I have to write the
> following code to read, for example, 3 lines. Is there a subroutine to
> read a given number of lines in an array?
>
> $line1=<IN>;
> $line2=<IN>;
> $line3=<IN>;

$ echo -e 'a\nb\nc\nd\ne\n' | perl -wle'

sub get_lines {
my ( $fh, $max, $aref )= @_;
my $count;
while ( ++$count <= $max and defined( my $line= <$fh> ) ) {
push @$aref, $line;
}
return $count;
}

my @lines;
get_lines +STDIN, 2, \@lines;
print for @lines;
'
a

b

--
Ruud