I/O means input/output, and is necessary to get computer programs to talk to the rest of the world.
Every Perl scripts starts out with three connections to the outside world:
In addition to these three filehandles, you can create your own.
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>);
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.
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 ...
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>);
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";
} |
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:
To print to standard error:print STDOUT "Hello world\n"; print "Hello world\n";
print STDERR "Does not compute.\n";
|
| Contents |
Next |