2013年3月23日土曜日

開発環境

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

その他参考書籍

4.

コード(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
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';

$^I = ".bak";
while(<>){
    $_ .= "## Copyright (C) 2013 by kamimura\n" if /^#!/;
    print $_;
}
$ ./sample.pl *.pl
$ ls *.pl *.pl.bak
hello_world.pl  sample.pl
hello_world.pl.bak sample.pl.bak
$ cat *.pl *.pl.bak
#!/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';

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

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

pythonの場合。

コード(BBEdit)

sample.py

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

import os, re, sys

for fname in sys.argv[1:]:
    name = "{0}.bak".format(fname)
    os.rename(fname, name)
    with open(name, 'r') as in_f:
        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:]:
    name = "{0}.bak".format(fname)
    os.rename(fname, name)
    with open(name, 'r') as in_f:
        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
#-*- coding: utf-8 -*-

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

import os, re, sys

for fname in sys.argv[1:]:
    name = "{0}.bak".format(fname)
    os.rename(fname, name)
    with open(name, 'r') as in_f:
        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!")
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import os, re, sys

for fname in sys.argv[1:]:
    name = "{0}.bak".format(fname)
    os.rename(fname, name)
    with open(name, 'r') as in_f:
        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
#-*- coding: utf-8 -*-

print("Hello, World!")
$

0 コメント:

コメントを投稿