您的位置:首页 > 其它

Perl 文件操作 (IO::File)

2015-06-15 10:59 459 查看
# author : ez
# date 2015/6/15
# describe : append write a file
#! perl

use strict;
use warnings;
use IO::File; # import symbols

my $fname = "./ko.pl";
my $fd = undef;

sub main () {
$fd = new IO::File $fname, O_WRONLY | O_APPEND; # write only and append
print $fd "this sentends;\n";  # write string into <$fd>
$fd -> setpos ($fd -> getpos);
undef $fd; # automatically close file handle
}

&main;

基本的读取:

sub rf () {
# or this
# my $fname = "> ./ko.pl"; then you can read without calling open ()
 my $fname = "./ko.pl";
$fd = new IO::File; # call constructor
$fd -> open ($fname);
print <$fd>; # print all content of this file
$fd -> close;
}

&rf;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  perl