Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Brief description of File::Temp module of Perl

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Require File::Temp

Use File::Temp (); use File::Temp qw/: seekable /

$fh = File::Temp- > new (); $fname = $fh- > filename

$fh = File::Temp- > new (TEMPLATE = > $template); $fname = $fh- > filename

$tmp = File::Temp- > new (UNLINK = > 0, SUFFIX = > '.dat'); print $tmp "Some data\ n"; print "Filename is $tmp\ n"; $tmp- > seek (0, SEEK_END)

Usage instructions script (for window and UNIX) script one, 01.pl

#! / usrbinbinAccording to Win and unix# wangxiaoyu#live.comuse strict;use warnings;# module declaration use File::Temp;use File::Spec;use IO::File;# is the default location where files are created, print File::Spec- > tmpdir (); print "\ n" # define the file handle, whether to delete the file after use, file suffix, such as my $fh = File::Temp- > new (TEMPLATE = > 'tmpXXXXX',UNLINK = > 0scoreSUFIX = >' .log', # where the default location for creating the file is re-specified DIR = > File::Spec- > tmpdir ()); my $FileName = $fh- > filename;# print the results of the tree command only to display the results print $fh "the following is the test content\ n", `tree` # by default, the file is still in the cache at this time. Use the flush function of IO::File to refresh the file to $fh- > flush (); print $FileName;print "\ n"; # print the contents of the file open (FH,$fh- > filename) or die "$!"; while () {print $_;} close (FH); script 2, 02.pl

#! / usrbinbinAccording to Win and Unix# wangxiaoyu#live.comuse strict;use warnings;# module declaration use File::Temp qw (tempfile); use File::Spec qw (tmpdir); use IO::File;# is the default location where files are created print File::Spec- > tmpdir (); print "\ n" # define the file handle, whether to delete the file after use, file suffixes such as my ($fh, $FileName) = tempfile (TEMPLATE = > 'tmpXXXXX',UNLINK = > 0 tree = >' .log'); # print the results of the tree command only to display the results on both windows and Unix print $fh "the following is the test content\ n", `tree`; print $FileName;print "\ n" # by default, the file is still in the cache at this time. Use the flush function of IO::File to refresh the file to $fh- > flush (); # print the contents of the file open (FH,$FileName) or die "$!"; while () {print $_;} close (FH); introduce new to create an object my $tmp = new File::Temp (); or my $tmp = File::Temp- > new () The parameters that can be followed are: TEMPLATE (or with a variable of at least four digits, denoted by X) the DIR SUFFIX UNLINK (0 or 1) method filename returns the file name of the temporary file followed by the above example my $FileName = $tmp- > filename; unlink_on_destroy controls whether the file is deleted after use $fh- > unlink_on_destroy (1) DESTROY deletes the file if the object is out of scope, which is similar to unlink_on_destroy unlink_on_destroy and DESTROY, and does not take effect when the variable $KEEP_ALL is true

The function tmpfile creates a temporary file use File::Temp qw (tempfile);. My $fh = tempfile (); or create a file where the directory is specified by the tempdir () function of the File::Spec module my ($fh, $filename) = tempfile ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report