2014年4月2日水曜日

開発環境

初めてのPerl 第6版 (Randal L. Schwartz (著)、brian d foy (著)、Tom Phoenix (著)、近藤 嘉雪 (翻訳)、オライリージャパン)、9章(正規表現によるテキスト処理)の9.6(練習問題)2.を解いてみる。

その他参考書籍

9.6(練習問題)2.

コード(BBEdit, Emacs)

sample207_2.pl

#!/usr/bin/env perl
# use diagnostics;
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDIN, ':utf8';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

my $filename = $ARGV[0];
die "usage: <inputfile>: $!" unless defined $filename;

open my $in_fh, '<', $filename or die "can't open '$filename: $!";
open my $out_fh, '>'. "${filename}.out" or
    die "can't open '${filename}.out: $!";

while (<$in_fh>) {
    s/fred/Larry/ig;
    print $out_fh $_;
}

close $in_fh;
close $out_fh;

入出力結果(Terminal)

$ ./sample207_2.pl temp.txt
$ cat temp.txt
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com
frederick
Wilma
Alfred
Wilma
fred flintstone
Wilma
Wilmaerick
AlWilma
Wilma flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
Fred and Wilma
Wilma and Fred
Fred and Wilma      
Wilma and Fred
Fred&Wilma
Mrs. Fred Flintstone   
I saw Fred yesterday
I, Fred!
Z
llama
fred
fredfredfred
fredwilmafred
$ cat temp.txt.out
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com
Larryerick
Wilma
AlLarry
Wilma
Larry flintstone
Wilma
Wilmaerick
AlWilma
Wilma flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
Larry and Wilma
Wilma and Larry
Larry and Wilma      
Wilma and Larry
Larry&Wilma
Mrs. Larry Flintstone   
I saw Larry yesterday
I, Larry!
Z
llama
Larry
LarryLarryLarry
LarrywilmaLarry
$ 

0 コメント:

コメントを投稿