my $person = Person.new(:life(100), :attack(1), :spell(2));
$person.weapons<a> = Weapon.new(:name<sword>, :powerLow(3), :powerHigh
+(5) );
$person.weapons<s> = Weapon.new(:name<spell>, :powerLow(0), :powerHigh
+(7) );
I suggest:
given my $person = Person.new(...) {
given .weapons {
.<a> = Weapon.new(...);
.<b> = Weapon.new(...);
}
}
Or simply:
given my $person = Person.new(...) {
.weapons = {
a => Weapon.new(...),
b => Weapon.new(...)
};
}
Or maybe just
my $person = Person.new(
...,
weapons => {
a => Weapon.new(...),
b => Weapon.new(...)
}
};
I personally think :pair() style should not be used for setting anything that's clearly key and value in a hash or object. :foo() is very nice for things that alter behaviour:
my $socket = Socket.new(
localport => 80,
localaddr => '127.0.0.1',
:proto<tcp> :listen(5) :timeout(500) :reuse
);
my @users = slurp '/etc/passwd' :chomp;
my @even = 2..10 :by(2);
All code is untested.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.