1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#!/usr/bin/env nqp
# based on blog post by Andrew Shitov:
#
# https://andrewshitov.com/2018/01/09/what-does-nqpgetattr-do/
#
# class C {
# has $.attr is rw;
# }
#
# my $o := nqp::create(C);
# $o.attr = 'other value';
# nqp::say(nqp::getattr($o, C, '$!attr')); # other value
class Foo {
has $!line;
has @!contents;
method set-line($value) {
$!line := $value;
}
method set-contents(@value) {
@!contents := @value;
}
method add2contents($value) {
@!contents.push($value);
}
method empty-contents() {
@!contents := [];
}
# note the empty parens are required in the declaration
method show-line() {
say(" \$!line: '$!line'");
}
# note the empty parens are required in the declaration
method show-contents() {
say(" \@!contents:");
if !nqp::elems(@!contents) {
say(" [empty]");
return;
}
for @!contents {
say(" $_");
}
}
}
my $line1 := ' # text ';
my $line2 := ' text ';
my $o1 := nqp::create(Foo);
$o1.set-line($line1);
my $o2 := nqp::create(Foo);
$o2.set-line($line2);
my $L1 := nqp::getattr($o1, Foo, '$!line');
my $L2 := nqp::getattr($o2, Foo, '$!line');
say("\$o1.line: '$L1'");
say("\$o2.line: '$L2'");
my @arr := nqp::list(
'elem 0',
'elem 1',
);
$o1.set-contents(@arr);
my @list := nqp::getattr($o1, Foo, '@!contents');
say("\$o1's contents:");
for @list {
say(" $_");
}
$o1.add2contents('another elem');
@list := nqp::getattr($o1, Foo, '@!contents');
say("\n\$o1's contents after an update:");
for @list {
say(" $_");
}
$o1.empty-contents;
@list := nqp::getattr($o1, Foo, '@!contents');
say("\n\$o1's contents after emptying:");
if !nqp::elems(@list) {
say(" \$o1's \@contents are empty");
}
else {
for @list {
say(" $_");
}
}
my $o3:= nqp::create(Foo);
$o3.set-contents(@arr);
$o3.set-line('line 3');
$o3.show-contents;
$o3.show-line;
|