開発環境
- OS X Mavericks - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs (Text Editor)
- Perl (プログラミング言語)
初めてのPerl 第6版 (Randal L. Schwartz (著)、brian d foy (著)、Tom Phoenix (著)、近藤 嘉雪 (翻訳)、オライリージャパン)、9章(正規表現によるテキスト処理)の9.6(練習問題)4.を解いてみる。
その他参考書籍
9.6(練習問題)4.
コード(BBEdit, Emacs)
sample208_4.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';
$^I = ".bak";
while (<>) {
if (/^#!/) {
$_ .= "## Copyright © 2014 by kamimura\n";
}
print;
}
入出力結果(Terminal)
$ cat 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;
$ cat sample208_3.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/\0/gi;
s/wilma/Fred/gi;
s/\0/Wilma/g;
print $out_fh $_;
}
close $in_fh;
close $out_fh;
$ ./sample208_4.pl sample207_2.pl sample208_3.pl
$ cat sample207_2.pl
#!/usr/bin/env perl
## Copyright ? 2014 by kamimura
# 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;
$ cat sample208_3.pl
#!/usr/bin/env perl
## Copyright ? 2014 by kamimura
# 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/\0/gi;
s/wilma/Fred/gi;
s/\0/Wilma/g;
print $out_fh $_;
}
close $in_fh;
close $out_fh;
$
0 コメント:
コメントを投稿