I am trying to create variable which belongs to object (non static) not class. In my following code I tried some things.
#!/usr/bin/perl -w
use Animal;
sub main{
$animal1 = new Animal();
$animal2 = new Animal();
for (my $i=0; $i < 10; $i++) {
$animal1->next_move();
$animal2->next_move();
}
print "\n";
}
main();
my animal class looks like this
#!/usr/bin/perl -w
#
#
# Animal.pl
package Animal;
sub new
{
my $class = shift;
my $self = {
_MOVE => 0,
};
bless $self, $class;
return $self;
}
sub next_move{
$self->{_MOVE}++;
print $self->{_MOVE}." ";
}
1;
my output is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
while my expected out was
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10