2013年8月3日土曜日

開発環境

『初めての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
## Copyright (C) 2013 by kamimura
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %files;

for  (@ARGV) {
    $files{$_} = 1;
}

while (<>) {
    if (/^## Copyright/) {
        delete $files{$ARGV};
    }
}

@ARGV = keys %files;

$^I = ".bak";

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

入出力結果(Terminal)

$ cat sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %files;

for  (@ARGV) {
    $files{$_} = 1;
}

while (<>) {
    if (/^## Copyright/) {
        delete $files{$ARGV};
    }
}

@ARGV = sort keys %files;

$^I = ".bak";

while (<>) {
    if (/^#!/) {
        $_ .= "## Copyright (C) 2013 by kamimura\n";
    }
    print;
}
$ cat sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-

import os, re, sys

for filename in sys.argv[1:]:
    os.rename(filename, filename + ".bak")
    with open(filename + ".bak") as in_f:
        with open(filename, "w") as out_f:
            for line in in_f.readlines():
                if re.search(r"^#!", line):
                    line += "## Copyright (C) 2013 by kamimura\n"
                out_f.write(line)
$ ./sample.pl sample.pl sample.py
$ cat sample.pl
#!/usr/bin/env perl
## Copyright (C) 2013 by kamimura
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %files;

for  (@ARGV) {
    $files{$_} = 1;
}

while (<>) {
    if (/^## Copyright/) {
        delete $files{$ARGV};
    }
}

@ARGV = sort keys %files;

$^I = ".bak";

while (<>) {
    if (/^#!/) {
        $_ .= "## Copyright (C) 2013 by kamimura\n";
    }
    print;
}
$ cat sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-

import os, re, sys

for filename in sys.argv[1:]:
    os.rename(filename, filename + ".bak")
    with open(filename + ".bak") as in_f:
        with open(filename, "w") as out_f:
            for line in in_f.readlines():
                if re.search(r"^#!", line):
                    line += "## Copyright (C) 2013 by kamimura\n"
                out_f.write(line)
$ cat sample.pl.bak
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %files;

for  (@ARGV) {
    $files{$_} = 1;
}

while (<>) {
    if (/^## Copyright/) {
        delete $files{$ARGV};
    }
}

@ARGV = sort keys %files;

$^I = ".bak";

while (<>) {
    if (/^#!/) {
        $_ .= "## Copyright (C) 2013 by kamimura\n";
    }
    print;
}
$ cat sample.py.bak
cat: sample.py.bak: No such file or directory
$

ちなみにpython3.3の場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-

import os, re, sys

for filename in sys.argv[1:]:
    os.rename(filename, filename + ".bak")
    with open(filename + ".bak") as in_f:
        with open(filename, "w") as out_f:
            for line in in_f.readlines():
                if re.search(r"^#!", line):
                    line += "## Copyright (C) 2013 by kamimura\n"
                out_f.write(line)

入出力結果(Terminal)

$ cat sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %files;

for  (@ARGV) {
    $files{$_} = 1;
}

while (<>) {
    if (/^## Copyright/) {
        delete $files{$ARGV};
    }
}

@ARGV = sort keys %files;

$^I = ".bak";

while (<>) {
    if (/^#!/) {
        $_ .= "## Copyright (C) 2013 by kamimura\n";
    }
    print;
}
$ cat sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-

import os, re, sys

filenames = []

for filename in sys.argv[1:]:
    with open(filename) as f:
        for line in f.readlines():
            if re.search("^## Copyright", line):
                break
        else:
            filenames.append(filename)

for filename in filenames:
    os.rename(filename, filename + ".bak")
    with open(filename + ".bak") as in_f:
        with open(filename, "w") as out_f:
            for line in in_f.readlines():
                if re.search(r"^#!", line):
                    line += "## Copyright (C) 2013 by kamimura\n"
                out_f.write(line)
$ ./sample.py sample.pl sample.py
$ cat sample.pl
#!/usr/bin/env perl
## Copyright (C) 2013 by kamimura
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %files;

for  (@ARGV) {
    $files{$_} = 1;
}

while (<>) {
    if (/^## Copyright/) {
        delete $files{$ARGV};
    }
}

@ARGV = sort keys %files;

$^I = ".bak";

while (<>) {
    if (/^#!/) {
        $_ .= "## Copyright (C) 2013 by kamimura\n";
    }
    print;
}
$ cat sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-

import os, re, sys

filenames = []

for filename in sys.argv[1:]:
    with open(filename) as f:
        for line in f.readlines():
            if re.search("^## Copyright", line):
                break
        else:
            filenames.append(filename)

for filename in filenames:
    os.rename(filename, filename + ".bak")
    with open(filename + ".bak") as in_f:
        with open(filename, "w") as out_f:
            for line in in_f.readlines():
                if re.search(r"^#!", line):
                    line += "## Copyright (C) 2013 by kamimura\n"
                out_f.write(line)
$ cat sample.pl.bak
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

my %files;

for  (@ARGV) {
    $files{$_} = 1;
}

while (<>) {
    if (/^## Copyright/) {
        delete $files{$ARGV};
    }
}

@ARGV = sort keys %files;

$^I = ".bak";

while (<>) {
    if (/^#!/) {
        $_ .= "## Copyright (C) 2013 by kamimura\n";
    }
    print;
}
$ cat sample.py.bak
cat: sample.py.bak: No such file or directory
$

0 コメント:

コメントを投稿