2013年3月24日日曜日

開発環境

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

その他参考書籍

5.

コード(BBEdit)

sample.pl

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

$^I = ".bak";
while(<>){
    $_ .= "## Copyright (C) 2013 by kamimura\n" if /^#!/;
    print $_;
}

入出力結果(Terminal)

$ ls *.pl
hello_world.pl sample.pl
$ cat *.pl
#!/usr/bin/env perl
## Copyright (C) 2013 by kamimura
use strict;
use warnings;
use utf8;

print "Hello, World!\n";
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %tmp;
for  (@ARGV) {
    $tmp{$_} = 1;
}
while (<>) {
    delete $tmp{$ARGV} if /^## Copyright/;
}
@ARGV = keys %tmp;
$^I = ".bak";
while(<>){
    $_ .= "## Copyright (C) 2013 by kamimura\n" if /^#!/;
    print $_;
}
$ ./sample.pl *.pl
kamimura$ ls *.pl *.pl.bak
hello_world.pl sample.pl sample.pl.bak
$ cat *.pl
#!/usr/bin/env perl
## Copyright (C) 2013 by kamimura
use strict;
use warnings;
use utf8;

print "Hello, World!\n";
#!/usr/bin/env perl
## Copyright (C) 2013 by kamimura
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %tmp;
for  (@ARGV) {
    $tmp{$_} = 1;
}
while (<>) {
    delete $tmp{$ARGV} if /^## Copyright/;
}
@ARGV = keys %tmp;
$^I = ".bak";
while(<>){
    $_ .= "## Copyright (C) 2013 by kamimura\n" if /^#!/;
    print $_;
}
$ cat *.pl.bak
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;

print "Hello, World!\n";
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %tmp;
for  (@ARGV) {
    $tmp{$_} = 1;
}
while (<>) {
    delete $tmp{$ARGV} if /^## Copyright/;
}
@ARGV = keys %tmp;
$^I = ".bak";
while(<>){
    $_ .= "## Copyright (C) 2013 by kamimura\n" if /^#!/;
    print $_;
}
$

python3.3の場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import os, re, sys

for fname in sys.argv[1:]:
    with open(fname) as in_f:
        for line in in_f:
            if re.search(r"^## Copyright", line):
                break
        else:
            in_f.seek(0)
            name = "{0}.bak".format(fname)
            os.rename(fname, name)
            with open(fname, 'w') as out_f:
                for line in in_f:
                    if re.search("^#!", line):
                        line += "## Copyright © 2013 by kamimura\n"
                    out_f.write(line)

入出力結果(Terminal)

$ ls *.py
sample.py test.py
$ cat *.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import os, re, sys

for fname in sys.argv[1:]:
    with open(fname) as in_f:
        for line in in_f:
            if re.search(r"^## Copyright", line):
                break
        else:
            in_f.seek(0)
            name = "{0}.bak".format(fname)
            os.rename(fname, name)
            with open(fname, 'w') as out_f:
                for line in in_f:
                    if re.search("^#!", line):
                        line += "## Copyright © 2013 by kamimura\n"
                    out_f.write(line)
#!/usr/bin/env python3.3
## Copyright © 2013 by kamimura
#-*- coding: utf-8 -*-

print("Hello, World!")
$ ./sample.py sample.py test.py
$ ls *.py *.py.bak
sample.py sample.py.bak test.py
$ cat *.py
#!/usr/bin/env python3.3
## Copyright © 2013 by kamimura
#-*- coding: utf-8 -*-

import os, re, sys

for fname in sys.argv[1:]:
    with open(fname) as in_f:
        for line in in_f:
            if re.search(r"^## Copyright", line):
                break
        else:
            in_f.seek(0)
            name = "{0}.bak".format(fname)
            os.rename(fname, name)
            with open(fname, 'w') as out_f:
                for line in in_f:
                    if re.search("^#!", line):
                        line += "## Copyright © 2013 by kamimura\n"
                    out_f.write(line)
#!/usr/bin/env python3.3
## Copyright © 2013 by kamimura
#-*- coding: utf-8 -*-

print("Hello, World!")
$ cat *.py.bak
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import os, re, sys

for fname in sys.argv[1:]:
    with open(fname) as in_f:
        for line in in_f:
            if re.search(r"^## Copyright", line):
                break
        else:
            in_f.seek(0)
            name = "{0}.bak".format(fname)
            os.rename(fname, name)
            with open(fname, 'w') as out_f:
                for line in in_f:
                    if re.search("^#!", line):
                        line += "## Copyright © 2013 by kamimura\n"
                    out_f.write(line)
$

0 コメント:

コメントを投稿