Mooseのaround modifierで正規表現を使う

| 0 Comments | 0 TrackBacks | このエントリーをはてなブックマークに追加 このエントリーのはてなブックマーク件数

around modifier を使うと、aroundで指定したメソッドの前後に処理を挟むことができます。要するに、AOPのinterceptorと同じですね。


最近、正規表現で書けるようパッチを送ったので、around modifireに正規表現が書けるようになります。正規表現にマッチする全てのメソッドにaround modifierを適用できるので、aroundが柔軟に書けるようになりますね。

{

    package Dog;
    use Moose;

    sub bark_once {
        my $self = shift;
        return 'bark';
    }

    sub bark_twice {
        return 'barkbark';
    }

    around qr/bark.*/ => sub {
        'Dog::around(' . $_[0]->() . ')';
    };

}

my $dog = Dog->new;
is( $dog->bark_once,  'Dog::around(bark)', 'around modifier is called' );
is( $dog->bark_twice, 'Dog::around(barkbark)', 'around modifier is called' );

まだ、色々と制約があるから、近々、もう少しパッチおくって柔軟に書けるようにするよ!


moooooooooooooooooooose!

No TrackBacks

TrackBack URL: http://perl-mongers.org/MT/mt-tb.cgi/6

Leave a comment

About this Entry

This page contains a single entry by dann published on May 22, 2008 10:17 PM.

もらったデータの構造がわかんないときは Data::Dumper におまかせ! was the previous entry in this blog.

Config::Multi で複数のコンフィグファイルをいい感じに読み込む is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 4.21-en