On Mar 25, 2015, at 7:44 AM, Tom Browder <[email protected]> wrote:
—snip--
> 1. How can I indirectly refer to the attributes in a method? The
> above doesn't work (with or without the '()’).
—snip—
use v6;
our %attrs = (
age => 1,
# wgt => 2, # Need to handle "No such method" before uncommenting.
);
class foo {
# has $.age = rw;
has $.age;
method all_attrs {
my @ordered_attrs = %attrs.sort({ + .value }).map({ .key });
for @ordered_attrs -> $k {
my $aval = self."$k"(); # supposed to work for a method name
say "attr { $k } has value '{ $aval }'";
}
}
}
my $f = foo.new( age => 30 );
# say $f.perl;
$f.all_attrs();
My modified version of your code (above) works for me. Output is:
attr age has value '30'
Note that your line "has $.age = rw" was invalid syntax,
and your "a" method was *outside* your original "foo" class.
—
Hope this helps,
Bruce Gray (Util of PerlMonks)