DeadPoet has asked for the wisdom of the Perl Monks concerning the following question:
I turn to the wisdom of the monks to help solve this issue. Ok, here is the problem:
In my example, the ZOO object MUST keep track of all the animals created. I would like ZOO to have a common constructor to create the Animals. In a real world program, the two Animals would have different attributes--not just the four that are defined. How do I setup and create such a structure? Please provide an example if possible.
Thanks In Advance For Your Help,
DeadPoet
#---------------------------------------------------------- # ------------- # | Zoo | # ------------- # | # | HASA Animal # v # ------------- # | Animal | # ------------- # ^^ # / \ # ISA Animal/ \ ISA Animal # / \ # / \ # ------------- ------------- # | Camel | | Lama | # ------------- ------------- #----------------------------------------------------------
The See Readmore for Code:
#--------------------------------------------------------- # Filename: Zoo.pm #--------------------------------------------------------- package Zoo::Zoo; use UUID; use lib '.'; use base qw( Zoo::Animal Zoo::Camel Zoo::Lama ); use strict; #--------------------------------------------------------- # Create the Class defaults. #--------------------------------------------------------- { my $_class_defaults = { _oid => '???', _type => 'zoo', _camel_count => 0, _lama_count => 0, _test1 => '????' }; sub _class_defaults { $_class_defaults } sub _class_default_keys { map { s/^_//; $_ } keys %$_class_defa +ults } } sub new { my ( $caller, %arg ) = @_; my $class = ref($caller); my $defaults = $class ? $caller : $caller->_class_defaults(); $class ||= $caller; my $self = bless {}, $class; # Generate an Object ID my $sref_oid = Zoo::Zoo->_gen_oid(); $self->{ _oid } = $$sref_oid; # Populate the new object with either passed parameters # or the defaults. foreach my $attrname ( $class->_class_default_keys ){ if ( exists $arg{ $attrname } ){ $self->{"_$attrname"} = $arg{$attrname}; } else { $self->{"_$attrname"} = $defaults->{"_$attrname"}; } } return $self; } sub _gen_oid { my ( $o_uuid, $o_id ); UUID::generate($o_uuid); UUID::unparse( $o_uuid, $o_id ); return undef if ( $o_id eq '' ); # catch if the unparse failed. return \$o_id; } sub _get_camel_count { my ( $self ) = @_; $self->{ _camel_count }; } sub _get_lama_count { my ( $self ) = @_; $self->{ _lama_count }; } sub _increment_camel { my ( $self ) = @_; $self->{ _camel_count } = $self->{ _camel_count } + 1; } sub _increment_lama { my ( $self ) = @_; $self->{ _lama_count } = $self->{ _lama_count } + 1; } sub add_camel { my ( $self ) = @_; my $o_camel = Zoo::Camel->new( ); $self->_increment_camel() if ( ref( $o_camel ) eq 'Zoo::Camel' ); return $o_camel; } sub add_lama { my ( $self ) = @_; my $o_lama = Zoo::Lama->new( ); $self->_increment_lama() if ( ref( $o_lama ) eq 'Zoo::Lama' ); return $o_lama; } sub print_obj { my ( $self ) = @_; foreach ( keys %{ $self } ) { print STDOUT "$_ -----> $self->{ $_ }\n"; } print STDOUT "\n\n"; } sub DESTROY { my ( $self ) = @_; printf ( "\n%s : $self cleaning up.\n", scalar ( localtime ) ); } 1; __END__ #--------------------------------------------------------- # Filename: Lama.pm #--------------------------------------------------------- package Zoo::Lama; @ISA = qw( Zoo::Animal ); use strict; sub new { my ( $class ) = @_; my $sref_oid = Zoo::Zoo->_gen_oid(); my $self = { _oid => $$sref_oid, _type => 'lama', _color => 'white', _legs => 4 }; bless $self, $class; return $self; } sub DESTROY { print "Destroying the lama Object\n"; } 1; __END__ #--------------------------------------------------------- # Filename: Camel.pm #--------------------------------------------------------- package Zoo::Camel; @ISA = qw( Zoo::Animal ); use strict; sub new { my ( $class ) = @_; my $sref_oid = Zoo::Zoo->_gen_oid(); my $self = { _oid => $$sref_oid, _type => 'camel', _color => 'grey', _legs => 4 }; bless $self, $class; return $self; } sub DESTROY { print "Destroying the camel Object\n"; } 1; __END__ #--------------------------------------------------------- # Filename: Animal.pm (Reserved Space for common Animal # Methods). #--------------------------------------------------------- package Zoo::Animal; use strict; 1; __END__ #--------------------------------------------------------- # Filename: zoo.pl #--------------------------------------------------------- use Zoo::Zoo; my $o = Zoo::Zoo->new(); for ( my $i = 1; $i<= 5; $i++ ) { my $o_camel = $o->add_camel(); my $o_camel = $o->add_lama(); } print STDOUT $o->{ _type } . " has " . $o->{ _camel_count } . " camels +\n"; print STDOUT $o->{ _type } . " has " . $o->{ _lama_count } . " lamas\n +";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating Common Constructor
by broquaint (Abbot) on Jul 10, 2003 at 15:03 UTC | |
by themage (Friar) on Jul 13, 2003 at 12:44 UTC | |
|
Re: Creating Common Constructor
by exussum0 (Vicar) on Jul 10, 2003 at 15:52 UTC | |
|
Re: Creating Common Constructor
by jaa (Friar) on Jul 10, 2003 at 15:16 UTC | |
|
Re: Creating Common Constructor
by DeadPoet (Scribe) on Jul 10, 2003 at 16:08 UTC | |
by jmanning2k (Pilgrim) on Jul 10, 2003 at 17:16 UTC | |
by DeadPoet (Scribe) on Jul 10, 2003 at 18:39 UTC | |
by jmanning2k (Pilgrim) on Jul 11, 2003 at 14:29 UTC | |
|
Re: Creating Common Constructor
by Flame (Deacon) on Jul 10, 2003 at 16:27 UTC | |
by exussum0 (Vicar) on Jul 10, 2003 at 20:24 UTC | |
|
Re: Creating Common Constructor
by smalhotra (Scribe) on Jul 10, 2003 at 16:25 UTC | |
|
Re: Creating Common Constructor
by Anonymous Monk on Jul 13, 2003 at 13:09 UTC |