in reply to Read from Input
When your script starts, it has the filehandle STDIN already available, for reading input from the keyboard (or other source, if the command interpreter you used to start the script specified a different source). So you can say <STDIN> or readline(STDIN) to read one or more lines of input. To tell it whether to read one or more lines, you put the call in a context: saying $x = <STDIN>; or print scalar(<STDIN>) is using it in scalar context, so only one line is read. Saying @x = <STDIN>; (which assigns lines to an array) or print <STDIN> is using it in list context so all available lines are read until end of file is reached (on the keyboard often specified by Control-Z or Control-D).
The notion of context is a difficult one for many perl beginners; basically, any operator or built-in function or subroutine will apply a specific context to each of its arguments/operands, sometimes based on what context the operator/built-in itself is in. This is mostly documented in perldoc perlop or perldoc perlfunc.
If you leave the filehandle out, the magical filehandle ARGV is used. It will read through any filenames specified as command line arguments to your script, or read from STDIN if no filenames were given.
|
|---|