ファイルからの出力、ファイルへの出力

#16 ファイルを扱ってみよう

あらかじめテストファイルを作成しておく

    # test.dat
    abc
    def
    ghi
    123
    456
    789

上記ファイルの内容を取得する

use strict;
use warnings;

# ファイル出力

open(my $in, "<" , "test.dat" ) or die ("could not open file.");

while(<$in>){
        print $_;

}

close($in);
    abc
    def
    ghi
    123
    456
    789

表示を確認

use strict;
use warnings;

# ファイル出力

open(my $out, ">" , "test2.dat" ) or die("Permission not found");

while(<$in>){
        print $out ;
}

close($out);

書き込みも試す。 test2.datファイルが同じディレクトリ以下に作成されたことを確認。

    #test2.dat
    abc
    def
    ghi
    123
    456
    789