如果我们不用Perl关闭文件有多严重? [英] How severe is it if we do not close a file in Perl?
问题描述
如果我们不用Perl关闭文件有多严重?
如果我在同一个程序中再次访问它,是否会影响程序或文件?
全局文件句柄将保留直到你的程序退出。这可能是不好的,但是因为你可能不应该使用全局文件句柄,所以这是一个没有问题的解决方案。
带有的词法文件句柄 close
d当它们的作用域保持不变时,它们的引用计数降到零。
如果文件句柄的名称被重用,则前一个文件句柄隐式地为 close
d。以下脚本重复使用相同的文件句柄来打印任意数量文件的前五行:
my $ fh;
foreach my $ filename(@ARGV){
打开$ fh,<,$ filename或者无法打开$ filename; #$ fh被重新使用
打印标量< $ fh> //下一个1 .. 5; #//是定义的或
}
在处理文件时,明确地关闭FH并不重要。但是,在进行IPC时很重要。
$ b 当
fork
ing时,所有未使用的文件句柄都应该被关闭,因为它们在分叉时被复制。这意味着在一个进程中关闭管道可能不会发送所需的EOF,因为相同的管道仍然在相关进程中打开。 这是一个演示在IPC中 close
的重要性:
管我的$ out,my $ in或者die $!
if(fork()){#PARENT
close $ out; #关闭未使用的句柄(重要!)
选择$ in;
$ | = 1; #设置$ in自动刷新(重要!)
$ SIG {PIPE} = sub {dieParent}; #死了,当管道关闭时
打印++ $ i,\\\
和sleep 1 while 1; #每秒打印一个数字
} else {#CHILD
close $ in; #关闭未使用的句柄
打印标量< $ out>为1.5; #从管道读取数字1到5
close $ out; #关闭管道(并触发SIGPIPE)
sleep 5; #等待,然后退出
dieChild;
$ b $ p
$ b 这个程序的输出是数字1到5.然后子关闭它结束于管道,触发父节点中的 SIGPIPE
。当父母死亡,孩子徘徊5秒,直到它也死。
这可以工作,因为父母关闭它的读取结束管道。如果 close $ out
从父项中移除, SIGPIPE
不会被触发,程序列印号码 ad infinitum 。
How severe is it if we do not close a file in Perl?
Will it affect the program or file if I access it again in the same program?
解决方案 Global filehandles will stay around until your program exits. This may be bad, but as you probably shouldn't be using global filehandles anyway, this is a non-issue.
Lexical filehandles with my
are close
d when their scope is left / their reference count drops to zero.
If the name of a filehandle is re-used, the previous filehandle is close
d implicitely. The following script reuses the same filehandle to print the first five lines of any number of files:
my $fh;
foreach my $filename (@ARGV) {
open $fh, "<", $filename or die "Can't open $filename"; # $fh is re-used
print scalar <$fh> // next for 1 .. 5; # // is the defined-or
}
When working with files, explicitely closing the FH is unimportant. However, it is critical when doing IPC. Closing a writing end to a pipe indicates EOF to the reading end.
When fork
ing, all unused filehandles should be closed, because they are duplicated while forking. This means that closing a pipe in one process might not be sending the desired EOF, because the same pipe is still open in a related process.
Here is a program that demonstrates the importance of close
in IPC:
pipe my $out, my $in or die $!;
if (fork()) { # PARENT
close $out; # close unused handle (important!)
select $in;
$| = 1; # set $in to autoflushed (important!)
$SIG{PIPE} = sub {die "Parent"}; # die, when the pipe is closed
print ++$i, "\n" and sleep 1 while 1; # print one number per second
} else { # CHILD
close $in; # close unused handle
print scalar <$out> for 1 .. 5; # read numbers 1 to 5 from the pipe
close $out; # close the pipe (and trigger SIGPIPE)
sleep 5; # wait, then exit
die "Child";
}
The output of this program is the numbers 1 to 5. Then the child closes its end to the pipe, triggering SIGPIPE
in the parent. While the parent dies, the child lingers around for 5 seconds until it dies too.
This works because the parent closed its reading end to the pipe. If close $out
is removed from the parent, SIGPIPE
would not be triggerd, and the program print numbers ad infinitum.
这篇关于如果我们不用Perl关闭文件有多严重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!