2012年9月11日火曜日

開発環境

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

3.

コード(TextWrangler)

sample.pl

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

my $in = $ARGV[0];
die unless defined $in;

my $out = $in;
$out =~s/(\.\w+)?$/.out/;

my $in_fh;
my $out_fh;
open $in_fh,"<",$in or die "can't open $in: $!";
open $out_fh,">",$out or die "can't write $out: $!";

while(<$in_fh>){
  s/fred/\0/gi;
  s/wilma/Fred/gi;
  s/\0/Wilma/g;
  print $out_fh $_;
}

入出力結果(Terminal)

$ ./sample.pl test.txt
$ cat test.txt
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com   
Fred
FRED
fred
frederick
Alfred
fred flintstone
Mr. Slate     
Mississippi
Bamm-Bamm    
wilama
barney
wilma and fred
fred and wilma
Wilma and Fred      
Fred and Wilma
wilma&fred
Mrs. Wilma Flintstone   
I saw Wilma yesterday
I, Wilma!
Z
llama
$ cat test.out
A
a
kamimura's blog
http://sitekamimura.blogspot.com
KMI
http://www.mkamimura.com   
Wilma
Wilma
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
$

0 コメント:

コメントを投稿