157 | | {{{#!perl |
158 | | use Foo; # use base qw(Class::Accessor::Fast); |
159 | | my $foo = Foo->new; |
160 | | $foo->hoge; # undef |
161 | | }}} |
| 157 | 1. Foo.pm |
| 158 | {{{#!perl |
| 159 | package Foo; |
| 160 | use base qw(Class::Accessor::Fast); |
| 161 | |
| 162 | __PACKAGE__->mk_accessors( qw(bar bazz) ); |
| 163 | |
| 164 | 1; |
| 165 | }}} |
| 166 | 1. main.pl |
| 167 | {{{#!perl |
| 168 | use Foo; |
| 169 | use feature say; |
| 170 | |
| 171 | my $foo = Foo->new( {bar => 1} ); |
| 172 | if (defined $foo->bar) { say $foo->bar; } |
| 173 | if (! defined $foo->bazz) { say "undef"; } |
| 174 | $foo->hoge; |
| 175 | }}} |
| 176 | 1. $ ./main.pl |
| 177 | {{{ |
| 178 | 1 |
| 179 | undef |
| 180 | Can't locate object method "hoge" via package "Foo" at main.pl line 7. |
| 181 | }}} |
| 182 | * 放り込んだハッシュリファレンスは元の構造のままアクセスできる |
| 183 | 1. main.pl |
| 184 | {{{#!perl |
| 185 | use Foo; |
| 186 | use feature say; |
| 187 | |
| 188 | my $foo = Foo->new( {hoge => 1, fuga => 2, piyo => { moge => [ a => b => c => 1 ] }} ); |
| 189 | if (defined $foo->bar) { say $foo->bar; } |
| 190 | |
| 191 | use Data::Dumper; |
| 192 | say Dumper $foo; |
| 193 | }}} |
| 194 | 1. $ ./main.pl |
| 195 | {{{ |
| 196 | $VAR1 = bless( { |
| 197 | 'piyo' => { |
| 198 | 'moge' => [ |
| 199 | 'a', |
| 200 | 'b', |
| 201 | 'c', |
| 202 | 1 |
| 203 | ] |
| 204 | }, |
| 205 | 'fuga' => 2, |
| 206 | 'hoge' => 1 |
| 207 | }, 'Foo' ); |
| 208 | |
| 209 | }}} |