本文共 2119 字,大约阅读时间需要 7 分钟。
require File::Temp;
use File::Temp qw/ :seekable /; $fh = File::Temp->new(TEMPLATE => $template); $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.dat' ); print $tmp "Some data\n"; print "Filename is $tmp\n"; $tmp->seek( 0, SEEK_END ); -
-
-
-
- use strict;
- use warnings;
-
- use File::Temp;
- use File::Spec;
- use IO::File;
-
-
- print File::Spec->tmpdir();
- print "\n";
-
-
- my $fh = File::Temp->new(
- TEMPLATE => 'tmpXXXXX',
- UNLINK => 0,
- SUFFIX => '.log',
-
- DIR => File::Spec->tmpdir()
- );
- my $FileName =$fh->filename;
-
-
- print $fh "以下是测试内容\n",`tree`;
-
-
- $fh->flush();
-
- print $FileName;
- print "\n";
-
-
- open(FH,$fh->filename) or die "$!";
- while(<FH>){
- print $_;
- }
- close(FH);
-
-
-
-
- use strict;
- use warnings;
-
- use File::Temp qw(tempfile);
- use File::Spec qw(tmpdir);
- use IO::File;
-
-
- print File::Spec->tmpdir();
- print "\n";
-
-
- my ($fh, $FileName) = tempfile(
- TEMPLATE => 'tmpXXXXX',
- UNLINK => 0,
- SUFFIX => '.log' );
-
-
- print $fh "以下是测试内容\n",`tree`;
-
- print $FileName;
- print "\n";
-
-
- $fh->flush();
-
-
- open(FH,$FileName) or die "$!";
- while(<FH>){
- print $_;
- }
- close(FH);
my $tmp = new File::Temp(); my $tmp = File::Temp->new(); TEMPLATE(或以跟至少四位的变量,用X表示) DIR SUFFIX UNLINK(0 或1) my $FileName = $tmp->filename; unlink_on_destroy 控制文件在使用过后是否删除 $fh->unlink_on_destroy(1); DESTROY 如果对象超出范围,会删除文件,这个与unlink_on_destroy 类似 unlink_on_destroy和DESTROY,在当变量$KEEP_ALL为真时,不会生效 use File::Temp qw(tempfile); 创建一个文件,目录由File::Spec模块的tempdir()函数指定 my ($fh, $filename) = tempfile(); 本文转自xiaoyuwang 51CTO博客,原文链接:http://blog.51cto.com/wangxiaoyu/523278,如需转载请自行联系原作者