天天看點

Perl 檔案操作 (IO::File)

# 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;