gpt4 book ai didi

perl - perl 中的文件句柄是全局的吗?

转载 作者:行者123 更新时间:2023-12-04 22:06:21 24 4
gpt4 key购买 nike

#!/usr/local/bin/perl
sub trial
{
open (LOGFILE, 'C:\Users\out.txt');
trial();
}
trial();

请忽略它会进入无限循环。

文件句柄 LOGFILE该方法是本地的还是私有(private)的?
如果没有,我怎样才能将其设为私有(private)/本地?
我知道 我的 .但我不知道如何在文件句柄上使用它。

最佳答案

这些文件句柄是全局的,因为它们是 typeglobs .这有时会导致非常糟糕的意外,因为您可能会意外覆盖在您正在使用的某个模块中定义的此类文件句柄。

如果你想要词法文件句柄,用 my 定义它们。像这样:

open my $fh, '<', 'C:\Users\out.txt';

也可以看看:
  • Which one is good practice, a lexical filehandle or a typeglob?
  • brian d foy on 'Why we teach bareword filehandles' in Learning Perl
  • 关于perl - perl 中的文件句柄是全局的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12617318/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com