R. Joseph Newton wrote:
>
> Got it!  The changes shown below seem to provide the protection I
> want.  I guess the object hash can be left as a programmer
> scratchpad, while the payload remains protected as a private variable

No, this is a step backwards I'm afraid!

> > package Person;
> >
> > use strict;
> > use warnings;
> > use Address;
> >
> > my ($surname, $given_name, $address);

These are class data, which is fine in itself, but they apply to all
instances of the class, and changing one object's address will change
them all. They are, after all, just

    $Person::surname
    $Person::given_name
    $Person::address

Each object's private data has to be in a separate anonymously
referenced data structure. Don't forget that you can have a
reference to an array or even a scalar if those structures are
more appropriate to your problem.

> > sub new {
> >   my $self = {};
> >   set_name(@_);

Best to call $self->set_name(@_) unless you particularly want to
avoid inheriting the method.

> >   bless $self;
> > }
> >
> > sub set_name {
> >   my $self = shift;
> >   ($surname, $given_name) = @_;

As I said, private data must be within the referenced data
structure.

    @{$self}{qw(surname givenname)} = @_;

> > }

The rest, as far as I can see, has only this one misconception.

> > sub print {
> >   my $self = shift;
> >   print "$surname, ";
> >   print "$given_name\n";
> >   return unless defined $address;
> >   $address->print();
> > }
> >
> > sub set_address {
> >   my $self = shift;
> >   $address = Address::new(@_);
> > }
> >
> > my $motto = "Every man, woman and child is a star";
> > __END__

[snip output]

Cheers Joseph




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to