[Melbourne-pm] Camel POOP -- initialisation of an object

Tim Connors tconnors+pmmelb at astro.swin.edu.au
Tue Nov 21 22:20:32 PST 2006


On Wed, 22 Nov 2006, Jacinta Richardson wrote:

> Tim Connors wrote:
>
> >   my ($class, %args) = (@_);
> >   $class = ref($class) || $class;
> >   my $self  = {
> >                _public => \%fields,
> >                %fields,
> >               };
>
> I'm not quite sure what this is supposed to be doing.  Why are you storing both
> a reference to %fields and the contents of %fields?  How will you keep them both
> in sync?

It comes in handy when you define a sub AUTOLOAD{}.  I can add all the
private fields to $obj that I want, but if it wasn't in %fields, then it
won't be in the hash held within $obj->_public, and AUTOLOAD{} says
"sorry, variable doesn't exist or is privatge; can't modify".  Poor
mans datahiding! (see man perltoot).

> >   my @keys = sort keys(%args);
> >   foreach my $key (@keys) {
> >     $self->$key($args{$key});   #initialise the known values
> >   }
> >   return $self;
>
> I'm not quite sure why you sort the keys before doing this loop, it doesn't seem
> to gain you anything.  Perhaps:
>
> 	foreach my $key ( keys %args ) {
> 		$self->$key($args{$key} );
> 	}
>

Yeah I know - I snipped some irrevant code.

-- 
Tim Connors
http://site.aao.gov.au/twc


More information about the Melbourne-pm mailing list