オブジェクト指向でファイルIO(IO::File)

OOでファイルIOを行うモジュールIO::Fileの基本的な使い方

オープンとクローズ

use IO::File;

my $fh = IO::File->new();
$fh->open("sample.txt", "r") or die $!;
$fh->close;

読み込み

1) 1行読み込み

use IO::File;

my $fh = IO::File->new("sample.txt", "r") or die $!;
my @lines = $fh->getline;
$fh->close;

2) 全行読み込み

use IO::File;

my $fh = IO::File->new("sample.txt", "r") or die $!;
my @lines = $fh->getlines;
$fh->close;

書き込み

1) 上書き

use IO::File;

my $fh = IO::File->new("sample.txt", "w");
$fh->print("sample message!!");
$fh->close;

2) 追記

use IO::File;

my $fh = IO::File->new("sample.txt", "a");
$fh->print("sample message!!");
$fh->close;