首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我应该如何使用Perl的File::Temp?

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

Stack Overflow用户
提问于 2010-10-18 21:16:56
回答 4查看 7.2K关注 0票数 11

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

问题是,我通常希望在写入文件之后调用外部程序之前对文件执行close操作,但如果我理解正确,close-ing tempfile()会导致该文件被删除。

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-10-18 21:24:52

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

代码语言:javascript
复制
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
票数 9
EN

Stack Overflow用户

发布于 2010-10-18 21:22:03

来自http://perldoc.perl.org/File/Temp.html

代码语言:javascript
复制
unlink_on_destroy

Control whether the file is unlinked when the object goes out of scope. The file is removed if this value is true and $KEEP_ALL is not.

   1. $fh->unlink_on_destroy( 1 );

Default is for the file to be removed.

尝试将其设置为0

票数 6
EN

Stack Overflow用户

发布于 2017-12-30 00:56:46

使用File::Temp的OOP接口,您可以:

代码语言:javascript
复制
my $cpp =  File::Temp->new;
print $cpp "SOME TEXT";
$cpp->flush;

`cat $cpp`;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3959532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档