Search code examples
perl

How many ways are there of getting stdin input in perl?


I have been learning perl, and find the motto interesting. It says there are multiple ways of doing anything. I know that you can use print and say to output stuff, but I haven't found any other way to read input than to use a predefined file pointer-like thing.

Are there any other ways?


Solution

  • This question is too broad.

    There are many different ways to read from a handle:

    • <>/readline (with $/ = $line_ending;)
    • <>/readline (with $/ = undef;)
    • <>/readline (with $/ = \$block_size;)
    • <>/readline (with $/ = "";)
    • getc
    • read
    • sysread
    • ...

    Then there are tools to work with terminals.

    • Term::ReadKey
    • Term::ReadLine
    • Curses
    • ...

    Also, handles can be represented in multiple ways.

    • A reference to an IO value (e.g. *STDIN{IO})
    • A glob containing an IO value (e.g. *STDIN)
    • A ref to a glob containing an IO value (e.g. \*STDIN)
    • The name of a glob containing an IO value (e.g. 'STDIN')
    • An IO::Handle object

    In one-liners, -n, -p, -l, -0, -F, -a and -C all have some connection to STDIN.

    ...