Add stevan's YAPC::NA talk
[gitmo/moose-website.git] / Moose_YAPC_Asia_2008_ja / practical_moose.s5
CommitLineData
720accfe 1Title: Moose
2Location: YAPC::Asia::2008
3Presenter: Yuval Kogman
4Date: 2008
5Theme: moose
6
7Moose はなにではないか
8======================
9Moose Is Not
10------------
11
12* 実験やプロトタイプ (experimental)
13* おもちゃ (toy)
14* もう一つのアクセサビルダー (accessor builder)
15* ソースフィルタ (source filter)
16* 黒魔術 (black magic)
17* Perl 6 in Perl 5
18
19✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
20
21Mooseとはなにか
22===============
23Moose Is
24--------
25
26* Perlのための完全にモダンなオブジェクトフレームワーク
27
28* A complete modern object framework for Perl
29
30✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
31
32Mooseとはなにか
33===============
34Moose Is
35--------
36
37* Class::MOPのためのシンタックスシュガー (Syntactic sugar)
38* 祖先たち (ancestry)
39 * CLOS (Common Lisp Object System)
40 * Smalltalk
41 * Alces latifrons
42 * Perl 6
43 * …
44* 安定していて、お仕事にもつかえます (Stable & Production ready)
45
46
47✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
48
49シンプルな例
50============
51A Simple Example
52----------------
53
54<pre><code>
55package Person;
56
57use strict;
58use warnings;
59
60sub new {
61 my ($class) = @_;
62
63 return bless {
64 name => '',
65 age => undef,
66 }, $class;
67}
68
69sub name {
70 my ($self, $name) = @_;
71 $self->{'name'} = $name if $name;
72 return $self->{'name'};
73}
74
75sub age {
76 my ($self, $age) = @_;
77 $self->{'age'} = $age if $age;
78 return $self->{'age'};
79}
80
811;
82</code></pre>
83
84✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
85
86シンプルなMooseの例
87===================
88A Simple Moose Example
89----------------------
90
91<pre><code>
92package Person;
93use Moose;
94
95has name => (is => 'rw');
96has age => (is => 'rw');
97
981;
99</code></pre>
100
101✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
102
103シンプルなMooseの例(つづき)
104===========================
105A Simple Moose Example (cont.)
106------------------------------
107
108* `use Moose;`
109 * キーワードをインポート (imports keywords)
110 * `use strict; use warnings;`
111 * `@ISA = qw(Moose::Object) unless @ISA`
112
113✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
114
115シンプルなMooseの例(つづき)
116===========================
117A Simple Moose Example (cont.)
118------------------------------
119
120* `has` はアトリビュートを定義する (declares attibutes)
121 * アクセサを生成 (generates accessors)
122 * `is => 'rw'` → 読み書き両用アクセサ
123 * `is => 'ro'` → 読み込み専用アクセサ
124 * `writer`, `reader`
125
126* `new` は `Moose::Object` から継承する
127
128
129##########
130
131今度はアトリビュートの機能を説明していくよ
132Now we're going to discuss more features of the attributes
133
134✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
135
136Mooseの例のバリエーション
137=========================
138Variations on a Moose Example
139-----------------------------
140
141<pre><code>
142package Person;
143use Moose;
144
145has name => (
146 is => 'rw',
147 isa => 'Str'
148 default => 'Bob'
149);
150
151has staff => (
152 is => 'ro',
153 isa => 'ArrayRef',
154 lazy => 1,
155 default => sub { [qw(Bob Alice Tim)] },
156);
157</code></pre>
158
159##########
160
161default と isa が追加されてます
162Adds default, isa
163
164✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
165
166Mooseの例のバリエーション(つづき)
167=================================
168Variations on a Moose Example (cont.)
169-------------------------------------
170
171* `default` は
172 * コードリファレンス (coderef)
173 * またはリファレンス以外 (数値, 文字列) (nonref)
174 * `new` にパラメータがわたされなかったときに使われる
175
176* `lazy` は `default` を遅延させる
177 * 最初に `$object->staff` が使われたときに呼ばれる (generates)
178 * `new` の中ではなく (no param)
179
180##########
181デフォルトの話
182discusses default
183
184リファレンスでないと想定外の共有がむずかしくなる
185non refs make accidental sharing hard
186
187✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
188
189Mooseの例のバリエーション(つづき)
190=================================
191Variations on a Moose Example (cont.)
192-------------------------------------
193
194* `isa` は型を規定する (specifies type)
195 * `Moose::Util::TypeConstraints`
196 * `Any, Item, Bool, Undef, Defined, Value, Num, Int, Str, Ref, ScalarRef, ArrayRef, HashRef, CodeRef, RegexpRef, GlobRef, FileHandle, Object, and Role`
197 * 型は存在する必要はありません (don't need to exist)
198<pre><code>
199 has 'date' => (isa => 'DateTime'); # DWIM
200</code></pre>
201##########
202
203isa, 型の制約
204isa, type constraints
205
206✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
207
208
209==
210Typical Family
211--------------
212
213* 型は階層構造をもっている (types have a hierarchy)
214 * `Item` ⊃ `Defined` ⊃ `Ref` ⊃ `Object`
215
216<pre><code>
217 subtype 'Ref'
218 => as 'Defined'
219 => where { ref($_) };
220
221 subtype 'Object'
222 => as 'Ref'
223 => where { blessed($_) }
224</code></pre>
225##########
226
227型の階層構造
228type hierarchy
229
230✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
231
232型の強制変換
233============
234Some Type of Coercion
235---------------------
236
237<pre><code>
238package Employee;
239use Moose;
240use Moose::Util::TypeConstraints;
241extends qw(Person);
242
243class_type 'Manager';
244
245coerce 'Manager'
246 => from 'Str'
247 => via { Manager->new( name => $_) };
248
249has manager => (
250 is => 'ro',
251 isa => 'Manager',
252 required => 1,
253 coerce => 1,
254);
255</code></pre>
256
257✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
258
259型の強制変換(つづき)
260====================
261Some Type of Coercion (cont.)
262-----------------------------
263
264<pre><code>
265# 型制約のキーワードをインポート(import type constraint keywords)
266use Moose::Util::TypeConstraints;
267
268
269# オブジェクトのサブタイプであるマネージャーを定義(define Manager, a subtype of Object)
270class_type "Manager";
271
272
273# 変換を定義する(define the conversion)
274coerce 'Manager'
275 => from 'Str'
276 => via { Manager->new( name => $_) };
277
278
279# アトリビュートごとに有効にする(enable it per attribute)
280has manager => (
281
282 coerce => 1,
283);
284</code></pre>
285
286##########
287
288例を細かく見ていくよ
289breakdown of the example
290
291クラスの型はMooseのクラスすべてに自動的に用意されます
292class types are automatically created for all Moose classes
293
294✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
295
296伝統的な委譲
297============
298Conventional Delegates
299----------------------
300
301<pre><code>
302package Employee;
303use Moose;
304extends qw(Person);
305
306has manager => (
307 is => 'ro',
308 isa => 'Manager',
309 handles => {
310 manager_name => 'name',
311 coworkers => 'staff',
312 }
313);
314</code></pre>
315
316* マネージャーは `Employee` のいくつかのメソッドを処理します
317* manager `handles` certain methods for `Employee`
318 * `$emp->coworkers` == `$emp->manager->staff `
319
320✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
321
322伝統的な委譲(つづき)
323====================
324Conventional Delegates (cont.)
325------------------------------
326
327<pre><code>
328has phone => (
329 ...
330 handles => [qw(number extension)],
331);
332</code></pre>
333
334✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
335
336伝統的な委譲(つづき)
337====================
338Conventional Delegates (cont.)
339------------------------------
340
341<pre><code>
342has phone => (
343 isa => "Phone"
344 handles => qr/$method_regex/,
345);
346</code></pre>
347
348* `Phone->meta->compute_all_applicable_methods`にフィルターをかける
349* Filters `Phone->meta->compute_all_applicable_methods`
350
351✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
352
353伝統的な委譲(つづき)
354====================
355Conventional Delegates (cont.)
356------------------------------
357
358<pre><code>
359has phone => (
360 ...
361 handles => "Dialing", # a role
362);
363</code></pre>
364
365✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
366
367伝統的じゃない委譲
368================
369UnConventional Delegates
370------------------------
371
372<pre><code>
373package Company;
374use Moose;
375use MooseX::AttributeHelpers;
376
377has employees => (
378 metaclass => 'Collection::Array',
379 isa => 'ArrayRef[Employees]',
380 is => 'rw',
381 provides => {
382 push => 'add_employee',
383 pop => 'remove_employee',
384 count => 'number_of_employees',
385 empty => 'any_employees',
386 }
387);
388</code></pre>
389✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
390
391メソッド変更のセカイ
392====================
393Modified Methods
394----------------
395
396<pre><code>
397before 'employees' => sub { warn 'calling employees' };
398
399after 'employees' => sub { warn 'finished calling employees' };
400</code></pre>
401
402* 現在のメソッドが実行される前/された後に実行されます
403* Pre/Post hooks
404 * `@_`のコピーを得ます(Get a copy of `@_`)
405 * 返り値は無視されます(Return value is ignored)
406
407✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
408
409メソッド変更のセカイ(つづき)
410============================
411Modified Methods (cont.)
412------------------------
413
414<pre><code>
415around 'employees' => sub {
416 my ($next, $self, @args) = @_;
417 ...
418 my @return = $self->$next(@args);
419 ...
420 return @return;
421};
422</code></pre>
423
424✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
425
426メソッド変更のセカイ(つづき)
427============================
428Modified Methods (cont.)
429------------------------
430
431<pre><code>
432package Employee;
433use Moose;
434
435sub do_work {
436 my $self = shift;
437
438 $self->punch_in;
439
440 inner(); # call subclass here
441
442 $self->punch_out;
443}
444</code></pre>
445
446✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
447
448メソッド変更のセカイ(つづき)
449============================
450Modified Methods (cont.)
451------------------------
452
453<pre><code>
454package Employee::Chef;
455use Moose;
456
457extends qw(Employee);
458
459augment do_work => sub {
460 my $self = shift;
461
462 while ( @burgers ) {
463 $self->flip_burger(shift @burgers);
464 }
465}
466
467$chef->do_work; # punch in, flip burgers, punch out
468</code></pre>
469✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
470
471型についての余談
472================
473Some Type of Digression
474-----------------------
475
476<pre><code>
477has employees => (
478 is => 'rw',
479 isa => 'ArrayRef[Employee]',
480);
481
482has shopping_carts => (
483 is => 'rw',
484 isa => 'ArrayRef[ArrayRef[ShinyBead]]'
485);
486</code></pre>
487
488##########
489
490型システムの機能についてちょっと説明していくよ
491Going to go into features of the type system for a bit
492
493パラメータ付きの型
494Parametrized types
495
496✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
497
498型についての余談(つづき)
499========================
500Some Type of Digression (cont.)
501-------------------------------
502
503<pre><code>
504has language => (
505 is => 'rw',
506 isa => 'English | Welsh | Scots | Gaelic',
507);
508
509has member => (
510 is => 'rw',
511 isa => 'Employee | ArrayRef[Employee|Group]',
512);
513</code></pre>
514
515##########
516
517型の結合
518Union types
519
520✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
521
522型についての余談(つづき)
523========================
524Some Type of Digression (cont.)
525-------------------------------
526
527<pre><code>
528package Foo;
529use Moose;
530use Moose::Util::TypeConstraints;
531use Test::Deep qw(eq_deeply ...);
532
533type 'SomethingTricky'
534 => where {
535 eq_deeply( $_, ... );
536 };
537
538has 'bar' => (
539 is => 'rw',
540 isa => 'SomethingTricky',
541);
542</code></pre>
543
544##########
545
546Test::Deppのカスタムバリデータ
547Test::Deep custom validator
548
549CPANからどんなバリデータでも持ってこられる
550Can use any validation from the CPAN
551
552✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
553
554パラメータ付きの型の強制変換
555============================
556Some Parametrized Type of Coercion
557----------------------------------
558
559<pre><code>
560use Moose::Util::TypeConstraints;
561subtype 'ArrayRef[Employee]' => as 'ArrayRef';
562
563coerce 'ArrayRef[Employee]'
564 => from 'ArrayRef[Str]'
565 => via { [ map { Employee->new( name => $_ ) } @$_ ] };
566
567has staff => (
568 is => 'ro',
569 isa => 'ArrayRef[Employee]',
570 lazy => 1,
571 default => sub { [qw(Bob Alice Tim)] },
572 coerce => 1,
573);
574</code></pre>
575
576##########
577
578ArrayRef[Str] から ArrayRef[Employee] に強制変換
579coerce parametrized ArrayRef[Employee] from ArrayRef[Str]
580
581強制変換は 'default' の返り値にも適用されます
582coercions can be applied to 'default' return values
583
584✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
585
586MooseのRole
587===========
588Role of the Moose
589-----------------
590
591* Role は…(A role is like a)
592 * JavaのInterfaceみたい
593 * mixinみたい
594 * …それでいて安全でパワフル(safe, powerful)
595* Role は小さくて再利用可能な動作向け(A role is for small reusable behaviors)
596 * 多重継承よりよい(better than using a multiple inheritence)
597
598✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
599
600MooseのRole(つづき)
601===================
602Role of the Moose (cont.)
603-------------------------
604
605* CPAN にある Role たち(Roles on the CPAN):
606 * `MooseX::Storage` - 柔軟なシリアライズ(Flexible serialization)
607 * `MooseX::LogDispatch` - `$self->logger->info("something happenned")`
608 * `MooseX::Getopt` - コマンドライン引数の処理(`@ARGV` aware constructor)
609 * `MooseX::Param` - `CGI.pm` の `param()` メソッドみたいなの(`param` method like `CGI.pm`'s)
610 * `MooseX::Clone` - 柔軟な`clone`メソッド(Flexible `clone` method)
611
612##########
613
614再利用可能な小さな動作の例
615Some examples of small reusable behaviors
616
617Paramは連携に便利
618Param is good for interacting with e.g. CGI::Expand or similar modules
619
620✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
621
622MooseのRole(つづき)
623===================
624Role of the Moose (cont.)
625-------------------------
626
627<pre><code>
628package Minion;
629use Moose;
630
631extends qw(Employee);
632
633with qw(Salaried::Hourly);
634
635package Boss;
636use Moose;
637
638extends qw(Employee);
639
640with qw(Salaried::Monthly);
641
642</code></pre>
643
644* `with` はクラスに Role を追加します
645* `with` adds roles into your class
646 * `Salaried::Hourly`が`Minion`に追加される
647 * `Salaried::Hourly` was added to `Minion`
648
649✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
650
651MooseのRole(つづき)
652===================
653Role of the Moose (cont.)
654-------------------------
655
656<pre><code>
657package Salaried;
658use Moose::Role;
659
660requires qw('paycheck_amount');
661</code></pre>
662
663* 単なるインターフェース
664* Just an interface
665
666✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
667
668MooseのRole(つづき)
669===================
670Role of the Moose (cont.)
671-------------------------
672
673<pre><code>
674package Salaried::Hourly;
675use Moose::Role;
676
677with qw(Salaried);
678
679has hourly_rate => (
680 isa => "Num",
681 is => "rw",
682 required => 1,
683);
684
685has logged_hours => (
686 isa => "Num",
687 is => "rw",
688 default => 0,
689);
690
691sub paycheck_amount {
692 my $self = shift;
693
694 $self->logged_hours * $self->hourly_rate;
695}
696
697</code></pre>
698
699* インターフェースよりイイネ!
700* More than an interface
701
702✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
703
704MooseのRole(つづき)
705===================
706Role of the Moose (cont.)
707-------------------------
708
709* Javaのインターフェースよりイイネ!
710* More than Java Interfaces
711 * インターフェースは挙動の'規定'を提供する
712 * Interfaces are behavior "contracts"
713 * Role は挙動の'実装'も提供できる
714 * Roles can also have code
715
716##########
717Roleはアトリビュートとメソッドを持てる
718roles can have attributes and methods
719Roleはインターフェースだけでなく動作を提供するもの
720roles provide behavior, not just interface
721
722✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
723
724MooseのRole(つづき)
725===================
726Role of the Moose (cont.)
727-------------------------
728
729* Roleの組み込み(Role Composition)
730 * 継承ではない(Not inheritence)
731 * 喧嘩両成敗(Symmetric)
732 * 順序は関係ない(Unordered)
733
734✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
735
736MooseのRole(つづき)
737===================
738Role of the Moose (cont.)
739-------------------------
740
741* Roleの組み込み(Role Composition)
742 * あいまいさが少ない(Less ambiguity)
743 * コンパイル時エラー(Compile time errors)
744 * …修正する方法もある(And ways to fix them)
745
746##########
747喧嘩両成敗というのは優先順位がないということ。ふたつのRoleが同じものを定義しようとした場合はコンパイル時にエラーになる(直さないといけない)
748symmetric composition means no precedence - if two roles try to define the same thing you get a compile time error that needs to be resolved
749多重継承の場合はだまって最初のクラスを使うもんだと想定してしまう
750multiple inheritence silently assumes you want the first class
751
752Roleは多重継承と違ってコンパイル時にエラーを吐く
753roles cause errors at compile time, unlike multiple inheritence
754
755Roleは簡単にエラーを修正する方法も用意している
756roles also provide easy ways to fix the errors
757
758✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
759
760MooseのRole(つづき)
761===================
762Role of the Moose (cont.)
763-------------------------
764
765<pre><code>
766package First;
767use Moose::Role;
768
769sub dancing { ... }
770
771package Second;
772use Moose::Role
773
774sub dancing { ... }
775
776package Foo;
777use Moose;
778
779# KABOOM
780with qw(
781 First
782 Second
783);
784</code></pre>
785
786##########
787
788衝突
789conflicts
790
791✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
792
793MooseのRole(つづき)
794===================
795Role of the Moose (cont.)
796-------------------------
797
798<pre><code>
799package Ent::Puppy;
800use Moose;
801
802with (
803 Tree => {
804 alias => {
805 bark => "tree_bark",
806 },
807 },
808 Dog => {
809 alias => {
810 bark => "bark_sound",
811 }
812 },
813);
814
815sub bark {
816 my $self = shift;
817
818 if ( $condition ) {
819 $self->tree_bark;
820 } else {
821 $self->bark_sound;
822 }
823}
824</code></pre>
825
826##########
827
828組み込むときにパラメータをつける
829Composition parameters
830衝突を解決するのも簡単だし
831Easier conflict resolution
832よりきめ細かいコントロールができるようになる
833Finer grained control
834
835✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
836
837MOPはキレイ
838===========
839MOPs Mean Cleanliness
840---------------------
841
842* Moose は Class::MOP でつくられてる
843* Moose is based on `Class::MOP`
844 * Perl5のためのメタオブジェクトプロトコル(Metaobject Protocol for Perl 5)
845 * すべてにオブジェクトがつくられる("makes an object for everything")
846
847<pre><code>
848my $class = $obj->meta; # $objのメタクラス($obj's metaclass)
849my $meta = MyApp->meta; # MyAppのメタクラス(MyApp's metaclass)
850my $emo = $obj->meta->meta # メタメタ(even more meta)!
851
852warn $obj->meta->name;
853</code></pre>
854
855✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
856
857内側からみてみる
858================
859Looking in From the Inside
860--------------------------
861
862<pre><code>
863my $metaclass = $self->meta;
864
865$metaclass->superclasses;
866
867$metaclass->linearized_isa; # すべての先祖クラスを得ます(returns all ancestors)
868
869$metaclass->has_method("foo");
870
871$metaclass->compute_all_applicable_methods; # すべてのメソッド(継承されたものもふくめて)(returns all methods (inherited too))
872
873$metaclass->has_attribute("bar");
874
875# … lots more
876</code></pre>
877##########
878
879simple introspection
880
881✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
882
883内側からみてみる(つづき)
884========================
885Looking in From the Inside (cont.)
886----------------------------------
887
888<pre><code>
889Moose::Meta::Class->create( Bar =>
890 version => '0.01',
891 superclasses => [ 'Foo' ],
892 attributes => [
893 Moose::Meta::Attribute->new( bar => ... ),
894 Moose::Meta::Attribute->new( baz => ... ),
895 ],
896 methods => {
897 calculate_bar => sub { ... },
898 construct_baz => sub { ... }
899 }
900);
901</code></pre>
902
903##########
904
905クラスはプログラム的につくることもできる
906Classes can be created programmatically
907
908無名クラスも可
909Anonymous classes also possible
910✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
911
912内側からみてみる(つづき)
913========================
914Looking in From the Inside (cont.)
915----------------------------------
916
917<pre><code>
918has foo => ( is => "rw" );
919
920__PACKAGE__->meta->add_attribute( foo => is => "rw" );
921</code></pre>
922
923* Mooseは単なるシュガー(Moose is just sugar)
924 * 大変な部分はMOPがしてくれる(The MOP does the hard work)
925
926✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
927
928メタクラスのタンゴ
929==================
930The Metaclass Tango
931-------------------
932
933* メタクラスはクラスの挙動をコントロールする
934* Metaclassses control class behavior
935
936<pre><code>
937has employees => (
938 metaclass => 'Collection::Array',
939 ...
940);
941</code></pre>
942
943* カスタムアトリビュートメタクラスは
944* custom attribute metaclasses
945 * アトリビュートがどういう風に動くかを変える
946 * change how attributes work
947* カスタマイズ可能なパーツたち
948* Many customizable parts
949 * `Moose::Meta::Class`, `Moose::Meta::Attribute, ``Moose::Meta::Method`, `Moose::Meta::Method::Accessor` `Moose::Meta::Instance`, `Moose::Meta::Role`, `Moose::Meta::TypeConstraint`, …,
950
951✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
952
953メタフレームを使う
954==================
955Working in the Meta Frame
956-------------------------
957
958* 仕事であったおもろい話(An interesting `$work` story)
959* flashを使ったサイト用のCMS(CMS for a flash website)
960* コンテンツはXML(Content is in XML)
961
962✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
963
964メタフレームを使う(つづき)
965==========================
966Working in the Meta Frame (cont.)
967---------------------------------
968
969* Step 1. use Moose
970* Step 2. ???
971* Step 3. 金(Profit)!
972
973✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
974
975メタフレームを使う(つづき)
976==========================
977Working in the Meta Frame (cont.)
978---------------------------------
979
980* Step 2.1. XMLスキーマ(schemas) → クラス(classes)
981 * 自動変換(Automatic conversion)
982 * MOPのおかげで楽勝(MOP makes it easy)
983 * 実行時には高レベルオブジェクト(High level objects in runtime)
984 * 裏ではXML(XML backed)
985 * クライアントのスキーマ(With client's schemas)
986 * SAX → Moose
987 * Moose → SAX
988
989✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
990
991メタフレームを使う(つづき)
992==========================
993Working in the Meta Frame (cont.)
994---------------------------------
995
996* Step 2.2. メタ記述(Meta descriptions)
997 * メタクラスを拡張(Extend the metaclasses)
998 * 追加の情報を埋め込む(Embed additional information)
999 * フィールド型(field types)
1000 * アクセスコントロール(access control)
1001
1002✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1003
1004メタフレームを使う(つづき)
1005==========================
1006Working in the Meta Frame (cont.)
1007---------------------------------
1008
1009* Step 2.3 イントロスペクションかわゆす(Introspection goodness)
1010 * 汎用Webフロントエンド(Generic web frontend)
1011 * オブジェクトイントロスペクションベース(Object introspection based)
1012 * HTMLビュー(view)
1013 * 編集用のウィジェット(Editing widgets)
1014 * クリーンで拡張性も高い(Clean, extensible)
1015
1016✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1017
1018Moose の欠点
1019============
1020Drawbacks of Moose
1021------------------
1022
1023* ロード時間(Load time)
1024 * `MooseX::Compile` がアルでよ(`MooseX::Compile` is in the works)
1025* いくつかの機能が遅い(Some features are slow)
1026 * でも、あなたがつかったぶんだけだから(but you only pay for what you use)
1027* hashref じゃないクラスの拡張はトリッキー(Extending non-Hash based classes is tricky).
1028 * でも可能(but possible): `MooseX::GlobRef::Object`
1029
1030✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1031
1032Mooseのイイ!とこ
1033================
1034Benefits of Moose
1035-----------------
1036
1037* 退屈なことを減らせる(Less tedious)
1038 * 決まり文句を書かなくていい(ditch that boilerplate):
1039 * アトリビュートのストレージ/アクセサ(attribute storage/access)
1040 * コンストラクタ(construction)
1041 * デストラクタ(destruction)
1042 * 引数検査(verification)
1043 * …
1044 * 繰り返しを減らせる(less repetition)
1045 * typo を減らせる(fewer typos)
1046
1047✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1048
1049Mooseのイイ!とこ(つづき)
1050========================
1051Benefits of Moose (cont.)
1052-------------------------
1053
1054* みじかい(Shorter)
1055 * declarative == 情報がおおく、タイプ数がすくない(more info, less typing)
1056 * no RSI ;-)
1057 * コードがすくなきゃバグもすくなかろう(less code means fewer bugs)
1058
1059✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1060
1061Mooseのイイ!とこ(つづき)
1062========================
1063Benefits of Moose (cont.)
1064-------------------------
1065
1066* テストがすくなくていい(Less testing)
1067 * Moose はよくテストされておる
1068 * Moose is well tested
1069 * アクセサやら挙動やらをチェックせんでもよろし
1070 * no need to check accessor behavior, etc
1071 * あなたのコードの目的にフォーカスできます!
1072 * focus on your code's purpose
1073 * きちんと「まとめて」おかなくてもいいよ
1074 * not that it is "assembled" correctly
1075 * http://c2.com/cgi/wiki?IntentionNotAlgorithm
1076
1077✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1078
1079Mooseのイイ!とこ(つづき)
1080========================
1081Benefits of Moose (cont.)
1082-------------------------
1083
1084* 読みやすい(More readable)
1085 * 宣言的なスタイルだからそのまま文書になっている
1086 * declarative style is self documenting
1087 * やりたいことを書け。関係ないOOのしかけとかはあまり書かなくてもいい
1088 * Code your intentions, not and OO mechanics less
1089
1090✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1091
1092Mooseのイイ!とこ(つづき)
1093========================
1094Benefits of Moose (cont.)
1095-------------------------
1096
1097* Meta object protocol
1098 * Perl の OO を綺麗にあつかえます
1099 * Cleans up all levels of Perl's OO
1100 * イントロスペクションできます
1101 * Provides introspection
1102 * パワフルな抽象化
1103 * Enables powerful abstractions
1104
1105✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1106
1107Mooseのイイ!とこ(つづき)
1108========================
1109Benefits of Moose (cont.)
1110-------------------------
1111
1112* 今年の流行だよ
1113* It's the new black
1114 * イカした連中はみんな#mooseにきている
1115 * All the cool kids hang out on #moose
1116 * かっこよさげなバズワード
1117 * Smart sounding buzzwords
1118 * 2007年にはRubyがそうだったね
1119 * Ruby is so 2007
1120
1121✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1122
1123おまけ
1124======
1125Bonus Material
1126--------------
1127
1128✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1129
1130Autobox
1131=======
1132
1133<pre><code>
1134package Units::Bytes;
1135use Moose::Role;
1136use Moose::Autobox;
1137
1138sub bytes { $_[0] }
1139sub kilobytes { $_[0] * 1024 }
1140sub megabytes { $_[0] * 1024->kilobytes }
1141sub gigabytes { $_[0] * 1024->megabytes }
1142sub terabytes { $_[0] * 1024->gigabytes }
1143
1144Moose::Autobox->mixin_additional_role(SCALAR => 'Units::Bytes');
1145</code></pre>
1146
1147✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1148
1149Autobox (つづき)
1150================
1151Autobox (cont.)
1152---------------
1153
1154<pre><code>
1155use Units::Bytes;
1156use Moose::Autobox;
1157
1158is(5->bytes, 5, '... got 5 bytes');
1159is(5->kilobytes, 5120, '... got 5 kilobytes');
1160is(2->megabytes, 2097152, '... got 2 megabytes');
1161is(1->gigabytes, 1073741824, '... got 1 gigabyte');
1162is(2->terabytes, 2199023255552, '... got 2 terabyte');
1163</code></pre>
1164
1165✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1166
1167perl -Moose
1168===========
1169
1170* Moose なワンライナーには `oose.pm` があるでよ
1171* Moose One Liners with `oose.pm`
1172
1173<pre><code>
1174perl -Moose -e'has foo => (is=>q[rw]); Class->new(foo=>1)'
1175</code></pre>
1176
1177* なんかためしたいときなどに
1178* Useful for testing if something works
1179* IRC での会話には欠かせませんな!
1180* Nice for IRC
1181
1182✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1183
1184MooseX::POE
1185===========
1186
1187<pre><code>
1188package Counter;
1189use MooseX::POE;
1190
1191has count => (
1192 isa => 'Int',
1193 is => 'rw',
1194);
1195
1196sub START {
1197 my ($self) = @_;
1198 $self->yield('increment');
1199}
1200
1201event increment => sub {
1202 my ($self) = @_;
1203 warn "Count is now " . $self->count;
1204 $self->count( $self->count + 1 );
1205 $self->yield('increment') unless $self->count > 3;
1206};
1207
1208Counter->new( count => 0 );
1209POE::Kernel->run();
1210</code></pre>
1211
1212* POE のコンポーネントを簡単にかけます
1213* POE components made easy
1214* それぞれのオブジェクトが POE::Session をもってます
1215* Every object has a POE::Session
1216* `event` がオブジェクトのステートを宣言します
1217* `event` declares object states
1218
1219✂------✂------✂------✂------✂------✂------✂------✂------✂------✂------
1220
1221Fin
1222===
1223
1224* Slides written by:
1225 * Chris Prather
1226 * Stevan Little
1227 * Robert Boone
1228
1229* Slides deleted by:
1230 * Yuval Kogman
1231
1232* Slides translated by:
1233 * tokuhirom