武蔵の日記 RSSフィード

2005 | 08 | 09 | 10 | 11 | 12 |
2006 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2008 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2009 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2010 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2011 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 11 |
2012 | 03 | 04 | 06 | 07 | 08 | 09 | 11 | 12 |
2013 | 01 |

October 11(Sat), 2008

Perlのオブジェクト指向

| 22:12 |  Perlのオブジェクト指向を含むブックマーク  Perlのオブジェクト指向のブックマークコメント

Perlオブジェクト指向をする方法を学ぶ。テキストは『すぐわかる オブジェクト指向 Perl』。Perlの偉い人もお勧めしていた本だよ。

対象とする読者(というかこれ自分用の備忘録)

Javaとかで簡単なオブジェクト指向を学んでいる人であれば、今回の自分用メモを読むとPerlでクラスを作成するポイントがわかる…と思います。はい。

静的なクラスを作る

静的なクラスというのはコンストラクタなんかでインスタンス化しなくても使えるクラス…というような雰囲気を感じる。違うかもしれないけれど。。。

とりあえず三角形の面積を求めるクラスを作るのだけれど、

  • Figureクラス
    • Triangleクラス
      • Regular Triangleクラス

というような継承関係をしているとして、以下のソースをご覧ください:

Figureクラス

use strict;
use warnings;

# Must specify the package name!
package Figure;

sub AUTOLOAD {
    my ($class, @rest) = @_;

    our $AUTOLOAD;

    warn "You are about to invoke $AUTOLOAD using the argument ",
        join(', ', @rest), ".\n";
    warn "But I don't know how to do it!\n";

    return "UNKNOWN";
}

1;

スーパークラスとして存在するクラス。packageで名前空間としてFigureを指定している。AUTOLOADは存在しないメソッド呼び出しがあったときに、それを引き受けるための専用のメソッド。

Triangleクラス

use strict;
use warnings;

# Must specify the package name!
package Triangle;
use base 'Figure';

# Must include these 3 lines below. It's a convention!

sub space {
    my ($class, $a, $b, $c) = @_;

    my $s = ($a + $b + $c) / 2;

    my $space = sqrt($s * ($s - $a) * ($s - $b) * ($s - $c));

    return $space;
}

1;

継承をするときは

use base 'module name';

もしくは

use base qw(module name);

とする。

RegularTriangleクラス

use strict;
use warnings;

# Must specify the package name!
package RegularTriangle;
use base 'Triangle';

sub space {
    my ($class, $a) = @_;

    $class->SUPER::space($a, $a, $a);
}

1;

継承している一つ上のクラスを指すには「SUPER」を用いる。

テスト用スクリプト

# === Libraries ===
use strict;
use warnings;

use Triangle;
use RegularTriangle;

# === Main part ===
my $a = 3;
my $b = 4;
my $c = 5;

my $space = Triangle->space($a, $b, $c);

print("Triangle: $space\n");


$a = 6;

$space = RegularTriangle->space($a);

print("Regular_Triangle: $space\n");

$space = Triangle->aaa($a);

クラスのメソッド呼び出しには「クラス名->メソッド名」を用いる。

.pmファイルを作るときのテンプレート(暫定)

use strict;
use warnings;

# Must specify the package name!
package xxx;

# Inheritance from ...
# use base qw(module name);

# If you use the module(s), you must put here:
use Carp qw(croak);    # extended debug module

# === Constructor ======================================
#    You can name the constructor any names you want.
#    But, it's a convention to name the constructor new.
# ======================================================
# sub new {
#     my ($class, $a, $b, $c) = @_;
#     my $self = {
#         a => $a,
#         b => $b,
#         c => $c,
#     };
#     bless $self, $class;
#     return $self;
# }

# === Deconstructor ===
# sub DESTROY {
# }

# ===============================================================
#    If you write a super class,
#    it is recommended that you should write an AUTOLOAD method.
# ===============================================================
# sub AUTOLOAD {
# }

1;

疑問点

  • privateがないけど、外部からアクセスできないメソッドを作りたいときはどうするの?
    • 無名サブルーチンを使うよ!…でも、なんか美しくない気がする。。。
すぐわかる オブジェクト指向 Perl

すぐわかる オブジェクト指向 Perl

ヘロンの公式(letを使う)

| 00:02 |  ヘロンの公式(letを使う)を含むブックマーク  ヘロンの公式(letを使う)のブックマークコメント

gaucheの勉強は文字列とか入出力の勉強をする前にちょっと寄り道。とりあえず手続き型言語の勉強をして、ソフトウェア開発試験対策とするつもり。Perlで色々やってたらヘロンの公式が出てきたから、それを作ってみるよ。

普通のプログラミング言語のローカル変数Lispではlambdaの仮引数に相当する。それで与えられた引数から、計算用の一時的な変数を用意する必要があるときとかはletを使うと幸せになれる…のだと思う。自分の認識では。ヘロンの公式の場合、

S = ¥sqrt{s(s - a)(s - b)(s - c)}

ただし

s = ¥frac{1}{2}(a + b + c)

とする。

このsをlet使って表現してあげれば楽ちんに書けるようになるはず(もしもletを使わなければ、sが出てくるたびに「(/ (+ a b c) 2)」を書かなければいけなくなる)。

gosh> (define (heron a b c)
	(let ((s (/ (+ a b c) 2)))
	  (sqrt (* s (- s a) (- s b) (- s c)))))
heron
gosh> (heron 3 4 5)
6.0

これで幸せになれたと思う。

トラックバック - http://d.hatena.ne.jp/sirocco634/20081011