gpt4 book ai didi

perl - 我应该如何使用 Perl 的 File::Temp?

转载 作者:行者123 更新时间:2023-12-04 01:57:59 24 4
gpt4 key购买 nike

我想创建一个临时文件,写入文件句柄,然后使用文件名调用外部程序。

问题是我通常想要 close写入文件后和调用外部程序之前的文件,但如果我理解正确 close -ing tempfile()导致它被删除。

那么这里的解决方案是什么?

最佳答案

在关闭缓冲的情况下写入临时文件。在 Perl 脚本中关闭文件之前调用外部程序,外部程序将能够读取您编写的所有内容。

use File::Temp qw(tempfile);
use IO::Handle;

my ($fh, $filename) = tempfile( $template, ... );

... make some writes to $fh ...

# flush but don't close $fh before launching external command
$fh->flush;
system("/path/to/the/externalCommand --input $filename");

close $fh;
# file is erased when $fh goes out of scope

关于perl - 我应该如何使用 Perl 的 File::Temp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3959532/

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