8.2 STDIN and Redirection

Up until this point we have assumed that is always going to be input that comes directly from the user.s keyboard. Users familiar with the concept of I/O redirection available in Linux and UNIX shells will be aware that it is also possible to redirect the output from either another program or use the contents of a file so that it appears to be keyboard input.

Input from a file can achieved using "<" I/O redirection on the command line when we invoke our Perl script. For example, assuming we had called the above example .showtext. and given it appropriate execute permissions it could be invoked as follows:

./showtext < /etc/passwd

Assuming you are on a Linux or UNIX based system this should result in the contents of the /etc/passwd file being displayed in the terminal window as if the user had typed it in on the keyboard.

Output from other programs can similarly be redirected as input to a Perl script using the pipe (|) command. For example to divert the output from the .ls. command:

ls -l | ./showtext

This will display the output from the ls -l command as though it too was typed by the user at the keyboard.