5.9. Using FilehandlesOnce a filehandle is open for reading, you can read lines from it the same way you can read from standard input with STDIN. So, for example, to read lines from the Unix password file: if ( ! open PASSWD, "/etc/passwd") { die "How did you get logged in? ($!)"; } while (<PASSWD>) { chomp; . . . } In this example, the die message uses parentheses around $!. Those are literal parentheses around the message in the output. (Sometimes, a punctuation mark is just a punctuation mark.) As you can see, what we've been calling the "line-input operator" is two components; the angle brackets (the real line-input operator) are around an input filehandle. A filehandle open for writing or appending may be used with print or printf, appearing immediately after the keyword but before the list of arguments: print LOG "Captain's log, stardate 3.14159/n"; # output goes to LOG printf STDERR "%d percent complete./n", $done/$total * 100; Did you notice that there's no comma between the filehandle and the items to be printed?[*] This looks especially weird if you use parentheses. Either of these forms is correct:
printf (STDERR "%d percent complete./n", $done/$total * 100); printf STDERR ("%d percent complete./n", $done/$total * 100); 5.9.1. Changing the Default Output FilehandleBy default, if you don't give a filehandle to print (or to printf as everything we say here about one applies equally well to the other), the output will go to STDOUT. But that default may be changed with the select operator. Here we'll send some output lines to BEDROCK: select BEDROCK; print "I hope Mr. Slate doesn't find out about this./n"; print "Wilma!/n"; Once you've selected a filehandle as the default for output, it will stay that way. But it's generally a bad idea to confuse the rest of the program, so set it back to STDOUT when you're done.[
select LOG; $| = 1; # don't keep LOG entries sitting in the buffer select STDOUT; # ...time passes, babies learn to walk, tectonic plates shift, and then . . . print LOG "This gets written to the LOG at once!/n"; ![]() |