"R. Joseph Newton" wrote: > > But... > > I can do this: > > #!/usr/bin/perl -w > > use strict; > > use Person; > > my $person = Person::new("Newton", "R. Joseph"); > $person->set_address("666 NoSpam way", "Eugene", "OR", "97402", > "USA"); > $person->{'Surname'} = "Johnson"; #this > $person->print_name(); > $person->{'What the heck'} = "Jeez, this is a porous guy!"; # and this > print "$person->{'What the heck'}\n"; > > Without anything preventing me. I am not at all sure that this is a good thing. Is > there any way to construct a class so that member data stays private? This was the > factor that kept me from venturing into Perl OOP, and it still seems like it could > lead to problems if the only thing preventing this kind of external access is the > injuction "Don't do that!". > > Joseph >
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 > package Person; > > use strict; > use warnings; > use Address; > > my ($surname, $given_name, $address); > > sub new { > my $self = {}; > set_name(@_); > bless $self; > } > > sub set_name { > my $self = shift; > ($surname, $given_name) = @_; > } > > 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__ > Results before change: E:\d_drive\perlStuff>PersonTest.pl Johnson, R. Joseph # Name has been corrupted by wayward code 666 NoSpam way Eugene, OR 97402 USA Jeez, this is a porous guy! After: Newton, R. Joseph # Still intact 666 NoSpam way Eugene, OR 97402 USA Jeez, this is a porous guy! With an additional line to demonstrate proper modification by object methods: #!/usr/bin/perl -w use strict; use Person; my $person = Person::new("Newton", "R. Joseph"); $person->set_address("666 NoSpam way", "Eugene", "OR", "97402", "USA"); $person->{'Surname'} = "Johnson"; # Still here, but doesn't touuch the payload $person->set_name("Clinton", "William"); # Safe modification--uses object method $person->print(); $person->{'What the heck'} = "Jeez, this is a porous guy!"; print "$person->{'What the heck'}\n"; And the output: Clinton, William 666 NoSpam way Eugene, OR 97402 USA Jeez, this is a porous guy! I like it. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]