Keep It Simple, Stupid | |
PerlMonks |
Re: Printing from stdin to stdoutby Athanasius (Archbishop) |
on Aug 26, 2019 at 02:48 UTC ( [id://11105017]=note: print w/replies, xml ) | Need Help?? |
Hello harangzsolt33, Angle brackets around a filehandle are a special Perl construct for calling the built-in readline function, which (like most Perl functions) behaves differently according to the context in which it appears. From the documentation: In scalar context, each call reads and returns the next line until end-of-file is reached, whereupon the subsequent call returns undef. In list context, reads until end-of-file is reached and returns a list of lines. The line my $L = <STDIN>; supplies a scalar context, and so only one line is read since readline is called only once. By contrast, the line print <STDIN>; supplies a list context (see print), so multiple lines may be read in by a single call. Change your second example as follows: my @L = <STDIN>; print @L; and you will see the same behaviour as in the first example. On Windows (where ^Z signals end-of-file):
Hope that helps,
In Section
Seekers of Perl Wisdom
|
|