I would say you need to make 'hair' part of the object's attributes
stored in the hash.

sub hair {
   my ($) = @_;
   $s->{hair} = "brown";
}

sub name {
   my ($s) = @_;
   printf "%s:%s\n", $s->{PERSON}, $s->{hair};
}

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Jeremy A
Sent: Friday, April 09, 2004 5:37 PM
To: [EMAIL PROTECTED]
Subject: Private Variables in OOP


Hi all,

I have an perl oop problem.

I want to manipulate/set a variable for one instance of an object
without 
changing the same variable for all instances of an object.

Thanks in advance for any help,

Regards,
Jeremy A.
------------------------------------------------------------------------
-----------------------------------
here is some a script which, when executed, demonstrates my problem.

my $n = Test->new( PERSON => "jer");
my $g = Test->new(PERSON => "jon");


while(1)
{
        $g->name();
        $n->name();
        $g->hair(); <------- this changes $hair var for both $g and $n
objects, 
but i want it to only change for $g.

}

package Test;

use strict;
use warnings;

my $hair = "n/a";


     sub new {
     my $invocant = shift;
     my $class = ref($invocant) || $invocant;
     my $self = { 'PERSON' => undef, @_, };

     return bless $self, $class;

     }


sub hair
{
$hair = "brown";
}


sub name
{
  my ($s) = @_;
  print $s->{PERSON},":$hair\n";
}



1; 

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to