这是一个展示不耐烦美德的好机会。
程序员喜欢将常量文字的重复分解,并将它们放入常量类型容器中。(但我将在这里简单地使用一个变量,以免分散对重要部分的注意力。)
use IO::File qw();
my $handle = bless(\*STDOUT => 'IO::File')
or die $OS_ERROR;
# Why not just `$handle = STDOUT`? Code above is necessary
# because using bare STDOUT invokes on IO::Handle only
# which does not have the binmode method.
⋮
$handle->binmode(1);
$handle->print('something');
这看起来不像是一场胜利,因为现在的代码比以前多得多。一旦您决定STDOUT
不再打印,而是打印到真实文件,就会获得丰厚的回报。或者,也许您想打开输出。那么你只需要更改一行代码而不是几行。
my $handle = IO::File->new('/var/log/cgi', 'a')
or die $OS_ERROR;
# method calls on $handle stay the same as before