yoda54 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; use Peasant; my $henry = Peasant->new(name => "Peasant1", hp => "15"); my $joe = Peasant->new(name => "Peasant2", hp => "15"); $joe->damage($henry->attack); $henry->damage($joe->attack);
package Peasant; use strict; sub new { my ($class, %arg) = @_; my $objref = { _name => $arg{name} || "unknown", _hp => $arg{name} || "unknown" }; bless $objref, $class; return $objref; } sub damage { print "$_[0] damage\n"; $_[0]->{_hp} - $_[0]; } sub name { return $_[0]->{_name} } sub attack { my $int = int(rand(5)); print "$_[0]->{_name} attacks for $int.....\n"; return $int; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl OO newbie advice - object interaction
by GrandFather (Saint) on Jun 21, 2006 at 05:10 UTC | |
by japhy (Canon) on Jun 21, 2006 at 11:28 UTC | |
|
Re: Perl OO newbie advice - object interaction
by jhourcle (Prior) on Jun 21, 2006 at 14:47 UTC | |
|
Re: Perl OO newbie advice - object interaction
by perl_lover (Chaplain) on Jun 21, 2006 at 05:05 UTC | |
by herveus (Prior) on Jun 21, 2006 at 11:57 UTC |