Perl 如何关闭文件写入中的选择?

Perl 如何关闭文件写入中的选择?,perl,Perl,我使用select将输出写入文件 open my $handler, ">","newfile.txt"; select ($handler); print "Something print into the file"; print "Something print into the file"; print "Something print into the file"; close $handler; #This is not working print "print into

我使用
select
将输出写入文件

open my $handler, ">","newfile.txt";
select ($handler);
print "Something print into the file";
print "Something print into the file";
print "Something print into the file";

close $handler;  #This is not working

print "print into the terminal";
为此,我将在不使用
的情况下轻松地选择

open my $handler, ">","newfile.txt";
print $handler " print on file";
print "print on terminal";
使用
选择
如何操作?

如何关闭
选择
?我还想在终端上打印输出

需要首先将默认文件句柄设置回原来的状态

my $old_fh = select($handle);    
# your prints ...
select $old_fh;
close $handle;
选择
设置新的默认文件句柄(如果提供了表达式),并返回当前表达式。因此,按照您的方式,您正在尝试关闭默认文件句柄

perldoc-f选择

选择文件句柄
挑选
返回当前选定的文件句柄。如果FILEHANDLE是 提供时,为输出设置新的当前默认文件句柄。这 有两种效果:第一,“写”或“打印”没有 filehandle默认为此filehandle。第二,提及 与输出相关的变量将引用此输出通道


需要首先将默认文件句柄设置回原来的状态

my $old_fh = select($handle);    
# your prints ...
select $old_fh;
close $handle;
选择
设置新的默认文件句柄(如果提供了表达式),并返回当前表达式。因此,按照您的方式,您正在尝试关闭默认文件句柄

perldoc-f选择

选择文件句柄
挑选
返回当前选定的文件句柄。如果FILEHANDLE是 提供时,为输出设置新的当前默认文件句柄。这 有两种效果:第一,“写”或“打印”没有 filehandle默认为此filehandle。第二,提及 与输出相关的变量将引用此输出通道