Perl/Object-Simple.md
... ...
@@ -0,0 +1,188 @@
1
+[[_TOC_]]
2
+
3
+# 概要
4
+- [MetaCPAN:Object::Simple](https://metacpan.org/pod/Object::Simple) を利用したオブジェクト指向プログラミングのサンプル。
5
+
6
+# 継承
7
+- Point クラスを継承した Point3D クラスには、追加のプロパティ(z)と追加されたプロパティが影響する部分のみを実装する。
8
+- new(), stringify(), abs() については継承元の Point クラスのメソッドが呼び出される。
9
+
10
+## ソース
11
+- Point.pm
12
+ ```perl
13
+ package Point;
14
+
15
+ use Object::Simple -base;
16
+ use overload '""' => \&stringify;
17
+
18
+ has label => '';
19
+ has x => 0;
20
+ has y => 0;
21
+
22
+ sub stringify {
23
+ my $self = shift;
24
+ return sprintf( '[%s](%s)', $self->label, join( ', ', $self->values ) );
25
+ }
26
+
27
+ sub values {
28
+ my $self = shift;
29
+ return ( $self->x, $self->y );
30
+ }
31
+
32
+ sub clear {
33
+ my $self = shift;
34
+ $self->x(0);
35
+ $self->y(0);
36
+ }
37
+
38
+ sub abs {
39
+ my $self = shift;
40
+ my $sum = 0;
41
+ map { $sum += $_ * $_ } $self->values;
42
+ return sqrt($sum);
43
+ }
44
+
45
+ 1;
46
+ ```
47
+
48
+- Point3D.pm
49
+ ```perl
50
+ package Point3D;
51
+
52
+ use Object::Simple 'Point';
53
+
54
+ has z => 0;
55
+
56
+ sub values {
57
+ my $self = shift;
58
+ return ( $self->SUPER::values, $self->z );
59
+ }
60
+
61
+ sub clear {
62
+ my $self = shift;
63
+ $self->SUPER::clear;
64
+ $self->z(0);
65
+ }
66
+
67
+ 1;
68
+ ```
69
+
70
+- use-Point.pl
71
+ ```perl
72
+ #!/bin/perl
73
+ # [GitHub:yuki-kimoto/Object-Simple](https://github.com/yuki-kimoto/Object-Simple)
74
+
75
+ use strict;
76
+ use warnings;
77
+ use utf8;
78
+ use Encode;
79
+ use Point;
80
+ use Point3D;
81
+ use open ':std' => ':locale';
82
+
83
+ print "\n# Point サンプル\n";
84
+ my $pointA = Point->new(
85
+ label => '点A',
86
+ x => 3,
87
+ y => 4,
88
+ );
89
+ print "${pointA}\n";
90
+ print "x: " . $pointA->x . "\n";
91
+ print "abs: " . $pointA->abs . "\n";
92
+ $pointA->y(9.3);
93
+ print "${pointA}\n";
94
+ $pointA->clear;
95
+ print "${pointA}\n";
96
+
97
+ print "\n# Point3D サンプル\n";
98
+ my $pointB = Point3D->new(
99
+ label => '点B',
100
+ x => 3,
101
+ y => 4,
102
+ z => 5,
103
+ );
104
+ print "${pointB}\n";
105
+ print "z: " . $pointB->z . "\n";
106
+ print "abs: " . $pointB->abs . "\n";
107
+ $pointB->z(9.6);
108
+ print "${pointB}\n";
109
+ $pointB->clear;
110
+ print "${pointB}\n";
111
+
112
+ exit;
113
+ ```
114
+
115
+## 出力
116
+```
117
+# Point サンプル
118
+[点A](3, 4)
119
+x: 3
120
+abs: 5
121
+[点A](3, 9.3)
122
+[点A](0, 0)
123
+
124
+# Point3D サンプル
125
+[点B](3, 4, 5)
126
+z: 5
127
+abs: 7.07106781186548
128
+[点B](3, 4, 9.6)
129
+[点B](0, 0, 0)
130
+```
131
+
132
+# インラインモジュール
133
+- 同一ファイル内でモジュールを定義する場合は、main ルーチンを実行する前にモジュール部分を実行しないとモジュールが正常に初期化されない。
134
+- main ルーチンより先にモジュール部分を実行するには、下記のいずれかを行う。
135
+ - main ルーチンの前でモジュール部分を定義する。
136
+ - main ルーチンを関数化し、モジュール定義後に main() を呼び出す。
137
+ - BEGIN ブロック内にモジュール定義を配置する。
138
+
139
+## ソース
140
+- inlineModule.pl
141
+ ```perl
142
+ #!/bin/perl
143
+
144
+ use strict;
145
+ use warnings;
146
+ use utf8;
147
+ use Encode;
148
+ use open ':std' => ':locale';
149
+
150
+ my $user = User->new(
151
+ id => '0001',
152
+ account => 'TaroY',
153
+ displayname => '山田 太郎',
154
+ );
155
+ print "${user}\n";
156
+
157
+ exit;
158
+
159
+ BEGIN {
160
+
161
+ package User {
162
+ use Object::Simple -base;
163
+ use overload '""' => \&stringify;
164
+
165
+ has id => '';
166
+ has account => '';
167
+ has displayname => '';
168
+
169
+ sub stringify {
170
+ my $self = shift;
171
+ return sprintf( '{id:"%s", account:"%s", displayname:"%s"}',
172
+ $self->id, $self->account, $self->displayname );
173
+ }
174
+ }
175
+ }
176
+ ```
177
+
178
+## 出力
179
+```
180
+{id:"0001", account:"TaroY", displayname:"山田 太郎"}
181
+```
182
+
183
+# リンク
184
+- [MetaCPAN:Object::Simple](https://metacpan.org/pod/Object::Simple) Simplest class builder, Mojo::Base porting, fast and less memory
185
+ - [GitHub:yuki-kimoto/Object-Simple](https://github.com/yuki-kimoto/Object-Simple) Simplest class builder, Mojo::Base porting, fast and less memory
186
+ - [Object::Simple](http://d.hatena.ne.jp/perlcodesample/20160912/1473688503) 簡単すぎるクラスビルダー、Mojo::Baseの移植、速くて省メモリ【日本語訳】 - Perlゼミ(サンプルコードPerl入門)
187
+
188
+- [MetaCPAN:overload](https://metacpan.org/pod/overload) Package for overloading Perl operations
Perl/_Sidebar.md
... ...
@@ -29,6 +29,7 @@
29 29
- [[MySQL-BIT]]
30 30
- [[NetWatcher]]
31 31
- [[nicoget]]
32
+- [[Object-Simple]]
32 33
- [[packBit]]
33 34
- [[Perl-Tidy]]
34 35
- [[readXSV]]