如何检查文件句柄是否在 Perl 中打开?

发布于 2024-07-12 13:40:22 字数 236 浏览 6 评论 0原文

有没有办法检查文件是否已在 Perl 中打开? 我想要读取文件访问权限,因此不需要 flock

 open(FH, "<$fileName") or die "$!\n" if (<FILE_IS_NOT_ALREADY_OPEN>);
 #  or something like
 close(FH) if (<FILE_IS_OPEN>);

Is there a way to check if a file is already open in Perl?
I want to have a read file access, so don't require flock.

 open(FH, "<$fileName") or die "$!\n" if (<FILE_IS_NOT_ALREADY_OPEN>);
 #  or something like
 close(FH) if (<FILE_IS_OPEN>);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷月断魂刀 2024-07-19 13:40:22

请参阅 Scalar::Util 中的有关 openhandle() 的答案。 我最初在这里写的答案曾经是我们能做的最好的答案,但现在已经严重过时了。

Please see the answer regarding openhandle() from Scalar::Util. The answer I originally wrote here was once the best we could do, but it's now badly outdated.

じее 2024-07-19 13:40:22

Scalar::Util 模块为此提供了 openhandle() 函数。 与 fileno() 不同,它处理与操作系统文件句柄无关的 perl 文件句柄。 与 tell() 不同,它在未打开的文件句柄上使用时不会产生警告模块的文档

openhandle FH

如果 FH 可以用作文件句柄并且已打开,或者 FH 是绑定句柄,则返回 FH。   否则返回“undef”。 

     $fh = openhandle(*STDIN);   # \*STDIN 
     $fh = openhandle(\*STDIN);   # \*STDIN 
     $fh = openhandle(*NOTOPEN);   # 未定义 
     $fh = openhandle("标量");   # 未定义 
  

The Scalar::Util module provides the openhandle() function for this. Unlike fileno(), it handles perl filehandles which aren't associated with OS filehandles. Unlike tell(), it doesn't produce warnings when used on an unopened filehandle From the module's documentation:

openhandle FH

Returns FH if FH may be used as a filehandle and is open, or FH is a tied handle. Otherwise "undef" is returned.

   $fh = openhandle(*STDIN);           # \*STDIN
   $fh = openhandle(\*STDIN);          # \*STDIN
   $fh = openhandle(*NOTOPEN);         # undef
   $fh = openhandle("scalar");         # undef
人事已非 2024-07-19 13:40:22

你为什么想这么做? 我能想到的唯一原因是当您使用旧式包文件句柄(您似乎正在这样做)并希望防止意外地将一个句柄保存到另一个句柄时。

这个问题可以通过使用新样式的间接文件句柄来解决。

open my $fh, '<', $filename or die "Couldn't open $filename: $!";

Why would you want to do that? The only reason I can think of is when you're using old style package filehandles (which you seem to be doing) and want to prevent accidentally saving one handle over another.

That issue can be resolved by using new style indirect filehandles.

open my $fh, '<', $filename or die "Couldn't open $filename: $!";
已下线请稍等 2024-07-19 13:40:22

Perl 正是为此目的提供了 fileno 函数。

编辑我对fileno()的目的进行了更正。 我确实更喜欢较短的测试

fileno FILEHANDLE

而不是

tell FH != -1

Perl provides the fileno function for exactly this purpose.

EDIT I stand corrected on the purpose of fileno(). I do prefer the shorter test

fileno FILEHANDLE

over

tell FH != -1

无声情话 2024-07-19 13:40:22

Tell 会产生一个警告(stat、-s、-e 等也是如此),并使用 use warnings (-w)

perl -wle '
    open my $fh, "<", "notexists.txt"; 
    print "can stat fh" if tell $fh
'
tell() on closed filehandle $fh at -e line 1.
-1

替代方案 fileno($fh)eof($fh) 不会产生警告。
我发现最好的选择是保存 open 的输出。

Tell produces a warning (so does stat, -s, -e, etc..) with use warnings (-w)

perl -wle '
    open my $fh, "<", "notexists.txt"; 
    print "can stat fh" if tell $fh
'
tell() on closed filehandle $fh at -e line 1.
-1

The alternatives fileno($fh) and eof($fh) do not produce warnings.
I found the best alternative was to save the output from open.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文