開発環境
- 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(練習問題)5.を解いてみる。
その他参考書籍
9.6(練習問題)5.
コード(BBEdit, Emacs)
sample208_5.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 %filenames;
for (@ARGV) {
$filenames{$_} = 1;
}
while (<>) {
if (/\A## Copyright/) {
delete $filenames{$ARGV};
}
}
@ARGV = keys %filenames;
$^I = ".bak";
while (<>) {
if (/\A#!/) {
$_ .= "## Copyright (C) 2014 by kamimura\n";
}
print;
}
入出力結果(Terminal)
$ cat sample208_3.pl
#!/usr/bin/env perl
## Copyright (C) 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;
$ cat 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;
}
$ ./sample208_5.pl sample208_3.pl sample208_4.pl
$ cat sample208_3.pl
#!/usr/bin/env perl
## Copyright (C) 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;
$ ls sample208_3.pl.bak
ls: sample208_3.pl.bak: No such file or directory
$ cat sample208_4.pl
#!/usr/bin/env perl
## Copyright (C) 2014 by kamimura
# 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;
}
$ cat sample208_4.pl.bak
#!/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;
}
$
0 コメント:
コメントを投稿