Perl has three select()
functions with widely differing purposes. One form takes four arguments and is a wrapper around the POSIX select(3)
call. A second form takes zero arguments and returns the currently selected file handlefilehandle: the handle of the output stream used by print
; it normally defaults to standard output. The third form takes one argument, a file handlefilehandle, and makes it the currently selected file handlefilehandle. That is, this form of select()
changes the file that is used by all print
statements (unless they specify their own file handlefilehandle).
Modifying the file handle filehandle used by print
is counterintuitive because subsequent print
statements will no longer print to standard output. Furthermore, the globally selected file handle filehandle is not garbage-collected; it remains open even if the file handle filehandle goes out of scope. Therefore, do not modify the selected file handle filehandle with select()
.
Noncompliant Code Example
...
This compliant solution avoids select()
and directs the print()
statement to use the log file handle filehandle for output.
Code Block | ||||
---|---|---|---|---|
| ||||
sub output_log { my $action = shift; open( my $log, ">>", "log.txt"); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year += 1900; $mon += 1; print $log "$year-$mon-$mday $hour:$min:$sec: $action\n"; } |
...
This noncompliant code example uses the one-argument select()
function temporarily to modify the autoflush property associated with the file. The one-argument select()
returns the old file handle filehandle (normally standard output). After the $log
file handle filehandle is selected, the modification of $|
instructs Perl to autoflush everything sent to the $log
file handle filehandle. That is, every output to the $log
file handle filehandle is flushed immediately. After the modification, select()
is called again to restore the original selected file handlefilehandle.
Code Block | ||||
---|---|---|---|---|
| ||||
select(( select($log), $| = 1)[0]); |
...