| 292 | |
| 293 | == OOP 関連 == |
| 294 | === 引数設定の自動化もどき === |
| 295 | * 意味があるのかどうかは知らん. |
| 296 | {{{ |
| 297 | #!perl |
| 298 | use strict; |
| 299 | use warnings; |
| 300 | use diagnostics; |
| 301 | use Carp; |
| 302 | |
| 303 | { |
| 304 | package Base; |
| 305 | |
| 306 | sub new ($%) { |
| 307 | my ($class, %argv) = @_; |
| 308 | my $self = bless {__class__ => $class}, $class; |
| 309 | return $self->__init__(%argv); |
| 310 | } |
| 311 | |
| 312 | sub __init__ ($%) { |
| 313 | my ($self, %argv) = @_; |
| 314 | |
| 315 | while (my ($key, $val) = each(%argv)) { |
| 316 | $self->{$key} = $val; |
| 317 | } |
| 318 | return $self; |
| 319 | } |
| 320 | |
| 321 | sub __class__ ($) { |
| 322 | my $self = shift; |
| 323 | return $self->{__class__}; |
| 324 | } |
| 325 | |
| 326 | sub __check__ ($@) { |
| 327 | my ($self, @requisite) = @_; |
| 328 | foreach (@requisite) { |
| 329 | Carp::confess $self->__class__(), " requires argument ", $_ |
| 330 | unless (defined($self->{$_})); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | 1; |
| 335 | } |
| 336 | |
| 337 | { |
| 338 | package Parent; |
| 339 | |
| 340 | use base qw(Base); |
| 341 | |
| 342 | sub __init__ ($%) { |
| 343 | my @requisite = qw(parentname); |
| 344 | |
| 345 | my ($self, %argv) = @_; |
| 346 | |
| 347 | $self->SUPER::__init__(%argv); |
| 348 | $self->SUPER::__check__(@requisite); |
| 349 | |
| 350 | return $self; |
| 351 | } |
| 352 | |
| 353 | sub parent ($) { |
| 354 | my $self = shift; |
| 355 | printf "parent is %s\n", $self->{parentname}; |
| 356 | } |
| 357 | |
| 358 | 1; |
| 359 | } |
| 360 | |
| 361 | { |
| 362 | package Child; |
| 363 | |
| 364 | use base qw(Parent); |
| 365 | |
| 366 | sub __init__ ($%) { |
| 367 | my @requisite = qw(childname); |
| 368 | |
| 369 | my ($self, %argv) = @_; |
| 370 | |
| 371 | $self->SUPER::__init__(%argv); |
| 372 | $self->SUPER::__check__(@requisite); |
| 373 | |
| 374 | return $self; |
| 375 | } |
| 376 | |
| 377 | sub child ($) { |
| 378 | my $self = shift; |
| 379 | printf "child is %s\n", $self->{childname}; |
| 380 | } |
| 381 | |
| 382 | 1; |
| 383 | } |
| 384 | |
| 385 | my $child = new Child(childname => "michael", parentname => "mike"); |
| 386 | $child->parent(); |
| 387 | $child->child(); |
| 388 | }}} |