Xiao Yafeng wrote:
> There are no barewords in Perl 6, but it seems new method is an exception:
>
> class Dog {
>
> has $name;
Attributes need to have a twigil, so it would be
has $.name
> method bark () {
> say $name;
> }
> }
> my $p = Dog.new($name => 'boo');
You haven't defined a variable $name in this scope, so you can't use on.
> $p.bark; #error!
> my $p = Dog.new( name => 'boo');
> $p.bark #say boo
the => automatically quotes the left hand side, just like in perl 5. I
see no barewords here.
This works fine with current rakudo:
class Dog {
has $.name;
method bark() {
say $.name;
}
}
my $x = Dog.new(name => 'boo');
$x.bark;
> more confused:
>
> class Dog {
>
> has @names;
> method bark () {
> say @names;
> }
> }
> my $p = Dog.new(names => 'boo');
> $p.bark; #nothing but passed.
>
> So, how set array attribute of a class by new method?
I'd expect this to work:
class Dog {
has @.names;
method bark() {
say @.names.join(', ');
}
}
my $x = Dog.new(:names<foo bar>);
# or: Dog.new(name => ('foo', 'bar'))
$x.bark;
but currently it doesn't in rakudo (in does in pugs though)
--
Moritz Lenz
http://moritz.faui2k3.org/ | http://perl-6.de/