Basic I/O

I/O means input/output, and is necessary to get computer programs to talk to the rest of the world.

The STDIN, STDOUT and STDERR Filehandles

Every Perl scripts starts out with three connections to the outside world:

STDIN
Standard input, used to read input. Initially connected to the keyboard, but can be changed from shell using redirection (<) or pipe (|).

STDOUT
Standard output, used to write data out. Initially connected to the terminal, but can be redirected to a file or other program from the shell using redirection or pipes.

STDERR
Standard error, used for diagnostic messages. Initially connected to the terminal, etc.

In addition to these three filehandles, you can create your own.

Reading Data from STDIN

To read a line of data into your program use the angle bracket function:


 $line = <STDIN>

<STDIN> will read one line of input from standard input and return it as the function result. You usually will assign the result to a scalar variable. The newline is not removed line automatically; you have to do that yourself with chomp:

 print "Type your name: ";
 $name = <STDIN>
 chomp $name;
 if ($name eq 'Jim Watson') {
    print "Hail great master!";
 else {
    print "Hello $name\n";
 }

The read/chomp sequence is often abbreviated as:

chomp($name = <STDIN>);

The Input Loop

At the "end of file" (or when the user presses ^D to end input) <STDIN> will return whatever's left, which may or may not include a newline. Thereafter, <STDIN> will return the undefined value.

This leads typical input loop:


 while ( $line = <STDIN> ) {
   chomp $line;
   # now do something with $line...
 }

The while loop will read one line of text after another. At the end of input, the angle-bracket operator returns undef and the while loop terminates. Remember that even blank lines are TRUE, because they consist of a single newline character.

The Default Input Variable

If you don't assign the result of the angle-bracket operator to a scalar variable, it will default to the special scalar variable $_. This scalar is the default for a number of other functions, including chomp and the regular expression match.

This example prepends the line number to its input.

Code:


 #!/usr/local/bin/perl
 # file: add_line_numbers.pl
  
 $line_number = 0;
 while ( <STDIN> ) {
   chomp;
   print $line_number++,": ",$_,"\n";
 }

Output:

(~) 50% add_line_numbers.pl <people.txt
0: Gabor Marth              gmarth@watson.wustl.edu  
1: Genome Sequencing Center
2: Washington University School of Medicine
3: 4444 Forest Park Blvd.
4: St. Louis, MO 63108
5: 314 286-1839
6: 314 286-1810 (fax)
7: Dates: Oct 17-23
8: 
9: Sean Eddy		 eddy@genetics.wustl.edu
10: Assistant professor
11: Department of Genetics
12: Washington University School of Medicine
13: 660 S. Euclid Ave.
14: St. Louis, Mo. 63110
15: 314 362-7666
16: 314 362-7855 (fax)
17: Dates: Oct 20-22
18: 
19: Warren Gish		 gish@sapiens.wustl.edu
...

Assigning to an Array

Normally you assign the angle-bracket function to a scalar variable, getting a line of input. What if you assign to an array? You get all the lines from the input file or terminal, one per array element!!!

It is convenient to pass this array to chomp, which will remove the newline from each member of the array.


 @lines = <STDIN>  # get all lines
 chomp @lines;           # remove all newlines

Or you can do both things in one elegant operation:

chomp(@lines = <STDIN>);

Reading Non-Line Oriented Data

To read data that isn't line-oriented (such as image data or other binary information), use the read function:

$bytes_read = read(FILEHANDLE,$scalar,$bytes_to_read);

This fragment reads 20 bytes at a time from standard input and prints it out.


 while ( read(STDIN,$data,20 ) {
   print "20 bytes => $data\n";
 }


Output

The print function writes data to output. In its full form, it takes a filehandle as its first argument, followed by a list of scalars to print:

print FILEHANDLE $data1,$data2,$data3,...

Notice there is no comma between FILEHANDLE and the data arguments. If FILEHANDLE is omitted it defaults to STDOUT (this can be changed). So these are equivalent:

print STDOUT "Hello world\n";
print "Hello world\n";
To print to standard error:
print STDERR "Does not compute.\n";

<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Tue Oct 12 07:07:29 EDT 1999