Object Oriented Programming in Perl

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Group

    Object Oriented Programming in Perl - Presentation Transcript

    1. Object Oriented Programming in Perl Tatsuhiko Miyagawa miyagawa @ bulknews .net Edge, Co.,Ltd. / Shibuya Perl Mongers LL Saturday 2003
    2. アジェンダ
      • Perl OO Basic
        • 名前空間と bless
        • 継承 / デストラクタ
        • AUTOLOAD/overload
      • Perl OO Advanced
        • Design Patterns
        • Mix-in, AOP
      • 事例紹介
    3. Perl OO Basic Object-oriented Perl is a small amount of additional syntax and semantics, added to the existing imperative features of the language. Damian Conway in “ Object Oriented Perl”
    4. クラス / オブジェクト
      • package と bless
        • クラスは名前空間 (package) で定義
        • メソッドはクラス内のサブルーチン
        • bless でリファレンスの実体をオブジェクトに
      package Dog; sub new { my($class, $name) = @_; bless { name => $name }, $class; } sub bark { my $self = shift; print “bark: $self->{name}”; } use Dog; my $dog = Dog->new(‘Snoopy’); $dog->bark();
    5. 継承
      • @ISA 配列で表現 (is-a)
        • base プラグマ使用を推奨
        • 多重継承も可能
      package Animal; sub run { my $self = shift; print “running!”; } package Dog; # @Dog::ISA = qw(Animal); use base qw(Animal); sub bark { } use Dog; my $dog = Dog->new(‘Snoopy’); $dog->bark(); # Dog::bark $dog->run(); # Animal::run
    6. デストラクタ
      • DESTROY メソッド
        • オブジェクト消滅時に実行
        • リファレンスカウンタ
      package Dog; sub DESTROY { my $self = shift; print “$self->{name}: Bye!”; } use Dog; my $dog1 = Dog->new(‘Scooby’); { my $dog2 = Dog->new(‘Scrappy’); } ! Scrappy: Bye! ! Scooby: Bye!
    7. AUTOLOAD
      • 未定義メソッドをキャッチ
        • アクセサの自動生成
        • メソッドのキャッシング・遅延ロード
      package Dog; use vars qw($AUTOLOAD); my $accessors = { name => 1 }; sub AUTOLOAD { my $self = shift; (my $method = $AUTOLOAD) =~ s/.*://; return $self->{$method} if $accessors->{$method}; die “Unknown method: $method” } my $dog = Dog->new(‘Snoopy’); my $name = $dog->name();
    8. 演算子オーバーロード
      • overload プラグマを使用
        • 5.6 以降は演算子が増えた
        • fallback オプション
      package Dog; sub new { my($class, $name, $weight) = @_; # … } use overload q(“”) => &name, q(0+) => &weight, fallback => 1; sub name { shift->{name} } sub weight { shift->{weight} } my $dog = Dog->new(“Snoopy”, 13.5); printf “name is %s, weight is %f”, $dog, $dog;
    9. UNIVERSAL
      • すべてのオブジェクトの祖先クラス
        • can(), isa() を定義
        • can() はメソッドのリファレンスを返す
        • AUTOLOAD と併用すると …
      my $dog = Dog->new(); $dog->isa(‘Dog’); # true $dog->isa(‘Animal’); # true $dog->isa(‘Man’); # false my $bark = $dog->can(‘bark’); $man->$bark(); package UNIVERSAL; sub AUTOLOAD { my $self = shift; } すべてのクラスの未定義メソッドはここに !
    10. 可視性
      • public, private, protected
        • 用意されてません
        • アトリビュートはアクセサメソッドで保護
        • private メソッドは _ を先頭に
      • paranoia な人は
        • caller() でチェック
        • Class::Fields, Attribute::Protected
    11. Perl OO Advanced
    12. Design Patterns (1/3)
      • Singleton Pattern
        • スタティック変数はレキシカル変数を利用
        • パッケージ変数に格納 (Class::Singleton)
      package Printer; my $instance = Printer->new(); sub instance { $instance } package Printer; use base qw(Class::Singleton); sub _new_instance { … } オブジェクトが 1 つしかないことは保証できない
    13. Design Patterns (2/3)
      • Decorator Pattern
        • AUTOLOAD を利用してメソッドを転送
        • Class::Decorator
        • Class::Delegate
      package ReverseDecorator; sub new { my($class, $obj) = @_; bless { o => $obj }, $class; } sub AUTOLOAD { my($self, @args) = @_; (my $meth = $AUTOLOAD) =~ s/.*://; my @new_args = map reverse($_), @args; $self->{o}->$meth(@new_args); }
    14. Design Patterns (3/3)
      • Class::* Modules
        • Class::Composite
        • Class::Prototype
      • Object:: PerlDesignPatterns
        • Perl でパターンやるなら必読
    15. Mix-in
      • 多重継承はダイアモンド型の問題あり
        • Exporter を利用して mix-in を実装
        • Ruby の Module に相当
        • mixin.pm (M.Schwern)
      package Speakable; use base qw(Exporter); @EXPORT = qw(speak); sub speak { my $self = shift; print “my name: “, $self->name; } package Man; use Speakable; sub speak { my $self = shift; print “my name: “, $self->name; } 実装を借りてくるようなイメージ
    16. AOP
      • Aspect Oriented Programming
        • Perl の OO は動的束縛
        • 他クラスのメソッドを上書き
        • Aspect.pm
      my $orig = Man->can(‘walk’); *Man::walk = sub { warn “Man::walk here”; goto &$orig; };
    17. おまけ (1/2)
      • rubyisms .pm
        • Simon Cozens 作
        • Steal some features from Ruby
      package Foo; use rubyisms; sub initialize { self->{things} = [ @_ ]; self->another_method; } sub my_method { if ($interesting) { … } else { super } } sub array_iterator (&@) { yield() for @_; } array_iterator { print $_[0], “ ” } (“Hello”, “World”);
    18. おまけ (2/2)
      • autobox .pm
        • use builtin datatypes as first-class objects
        • Perl 5.8.1 RC4 へのパッチ必要
      use autobox; my $range = 10->to(1); my $ton = $range->[0]->mul(10); my $error = 3.14->minus(22/7); my $greeting = "Hello, World"->upper(); $greeting->to_lower(); $greeting->for_each(&char_handler); my $arr = [ @_ ]->map(...)->sort(...);
    19. Perl の OO は現場で使えるか ?
    20. 事例紹介
      • Sledge ( http:// sl .edge. jp / )
        • MVC な Web アプリケーションフレームワーク
        • オブジェクト指向
          • アプリケーション: クラス
          • ページ: メソッド
        • Abstract Factory, Strategy, Singleton, Template Method などのパターンを利用
        • Class::DBI (O/R Mapping) とコンボが強力
    21. Sledge の適用事例
      • エッジ社ではほぼ全てのプロジェクトで利用
        • プロジェクトあたりプログラマ 2-4 名
        • 開発期間 2w-6w
      • 2001 年夏に開発 2002 年始めから標準化
        • 浸透には時間がかかった
          • Sledge 自体の安定性の問題もあり
        • チーフエンジニアへの教育
          • 各プロジェクトで採用-> OJT とノウハウ共有 (CVS, Wiki, Mailing List)
    22. まとめ
      • 基本的 OO 機能は備わっている
      • 何でも動的
        • $obj->$method();
        • push @{“$class::ISA”}, $another;
      • Hack すれば大体のことは実現できる
        • 何をしてるかわかる人がやる
        • 何をしてるかわからないようにする

    + sekimurasekimura, 3 years ago

    custom

    1913 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1913
      • 1913 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events