Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.1 Reading Input from Standard Input

Input is read from the user via the input stream. In simplest terms you can think of the operator as the user's keyboard although on Linux and UNIX systems output from other sources can be redirected to the stream of your Perl program. This topic will be covered later in this chapter but for now lets assume the keyboard as .

The most basic use of the operator can be expressed as follows:

#!/usr/bin/perl $userinput = <STDIN>; chomp ($userinput); print "User typed $userinput\n";
This script, when executed, will wait for the user type to something and then display that input after the keyboard key is pressed. The chomp() command is used here to strip off the trailing carriage return giving us just the text that was entered. In a real world application you would do something meaningful with this input but for the sake of this example we will just use the print command to display whatever the user entered.

As you have probably come to expect with Perl there are many ways that the operator can be used. For example we don.t have to read a single line of user input and can easily read multiple lines into an array:

#!/usr/bin/perl @userinput = <STDIN> foreach (@userinput) { print; }
The above example will continue to read lines until the End of File (EOF) control character is encountered. On a Linux or UNIX based system this is typically represented by the Ctrl-D key. To find out what your EOF character is on a Linux or UNIX system run the stty as follows in a shell window:

stty -a

stty will display the current setting for your terminal window. Look for the .eof. entry which will typically be configured by default as .^D. (i.e the D key pressed whilst holding down the Crtl key).



 
 
  Published under the terms of the GNU General Public License Design by Interspire