class Point {
has Str $!name;
has Int $.x;
has Int $.y;
method !to-center {
sqrt($!x ** 2 + $!y ** 2);
}
method distance-from-center {
self!to-center;
}
method set-name(Str $name) {
$!name = $name;
}
}
my $point = Point.new(x => 3, y => 4);
$point.set-name('Point 1');
say $point.distance-from-center;
say $point.x;
say $point.y;
class Circle {
has Point $.center;
has Int $.radius = 1;
method area {
pi * $!radius ** 2;
}
}
my $circle = Circle.new(
center => Point.new(x => 5, y => 10),
radius => 50
);
say $circle.area;
class Mammal {
has $.name;
has $.nourishment-type = "milk from mother's mammary glands";
method sound {
say "mammal makes sound";
}
}
role Pet {
method is-companion { True }
}
role Shepherd {
method does-herd { True }
}
class Dog is Mammal does Pet does Shepherd {
has $.name = 'Doggy';
method sound {
say "woof woof";
}
}
class Cat is Mammal does Pet {
has $.name = 'Cattie';
method sound {
say "meow";
}
}
my $cornie = Dog.new(name => 'Cornie');
say $cornie.name;
$cornie.sound;
my $prosia = Cat.new(name => 'Prosia');
say $prosia.name;
$prosia.sound;
if $cornie.is-companion {
my $output = $cornie.name ~ " is a pet.";
$output ~= " And is herding the sheep!" if $cornie.does-herd;
say $output;
}
if $prosia.is-companion {
say $prosia.name, " is a pet";
}
class Dog {
has $.name;
my $.counter;
method new ($newName) {
$.counter++;
self.bless(name => $newName);
}
}
my $dog = Dog.new("yayaya");
say $dog.name;
say Dog.counter;
class Human {
has $.name;
has $.age;
has $.sex;
has $.nationality;
has $.eligible;
method assess-eligibility {
if self.age < 21 {
$!eligible = 'No'
} else {
$!eligible = 'Yes'
}
}
}
my $john = Human.new(name => 'John', age => 23, sex => 'M', nationality => 'American');
$john.assess-eligibility;
say $john.eligible;
my $name="czq";
my $human = Human.new(:$name);
say $human.name;
class Point {
has $.x;
has $.y;
multi method new($x, $y) {
self.bless(:$x, :$y);
}
}
my $p = Point.new(-1, 1);
say $p.x;
class Enemy {
method attack-with-arrows { say "peow peow peow" }
method attack-with-swords { say "swish cling clang" }
method attack-with-fireball { say "sssSSS fwoooof" }
method attack-with-camelia { say "flap flap RAWWR!" }
}
my $selector = { .name ~~ /^ 'attack-with-' / };
given Enemy.new -> $e {
my $attack-strategy
= $e.^methods().grep($selector).pick();
$e.$attack-strategy();
}
class Chemical {
has $.formula;
method gist {
my $output = $!formula;
$output ~~ s:g/(<[0..9]>)/{(0x2080+$0).chr}/;
$output;
}
}
class Parent {
method frob {
say "the parent class frobs"
}
}
class Child is Parent {
method frob {
say "the child's somewhat more fancy frob is called"
}
}
my Parent $test;
$test = Child.new;
$test.frob;
class Employee {
has $.salary is rw = 1000 ;
has %.hash is rw = 'Perl' => 5, 'Takudo' => 6;
method pay() {
say "Here is \$$.salary";
}
}
class Programmer is Employee {
has @.known_languages is rw;
has $.favorite_editor;
method code_to_solve( $problem ) {
$.salary = 100;
%.hash{'ha'} = 'haha';
say %.hash{'ha'};
say "Solving $problem using $.favorite_editor in "
~ $.known_languages[0] ~ '.' ~ ' with slary ' ~ $.salary;
}
}
my $programmer = Programmer.new(
salary => 100_000,
known_languages => <Perl5 Perl6 Erlang C++>,
favorite_editor => 'vim'
);
$programmer.code_to_solve('halting problem');
$programmer.pay();
class Dog { method legs { 4 } }
class Dog { method legs (Dog:) { 4 } }
class Dog { method legs (::?CLASS:) { 4 } }
class Dog { method legs (Dog:U:) { 4 } }
class Dog { method legs (::?CLASS:U:) { 4 } }
class Cat { method legs (::?CLASS:U:) { 4 } }
role with-method {
method a-method { return "in-a-method of " ~ $?CLASS.^name };
}
class a-class does with-method {
method another-method { return 'in-another-method' };
}
class b-class does with-method {};
my $what-class='a-class';
say ::($what-class).a-method;
$what-class = 'b-class';
say ::($what-class).a-method;
my $what-method='a-method';
say a-class."$what-method"();
$what-method='another-method';
say a-class."$what-method"();
my $ⲧ = " " xx 4;
class Journey {
has $.origin;
has $.destination;
has @.travelers;
has Str $.notes is rw;
multi method notes() { "$!notes\n" };
multi method notes( Str $note ) { $!notes ~= "$note\n$ⲧ" };
method Str { "⤷ $!origin\n$ⲧ" ~ self.notes() ~ "$!destination ⤶\n" };
}
my $trip = Journey.new( :origin<Here>, :destination<There>,
travelers => <þor Freya> );
$trip.notes("First steps");
$trip.notes("Almost there");
print $trip;
class Department {
has @.employees;
has $.name;
method gen_print_info {
return “$.name:\n” ~ “\t\t” ~ @.employees.sort.join(“, ”)
}
}
my @company = (
Department.new(name => ‘Accounting’,
employees => <Jeff Jane Susan>),
Department.new(name => ‘Security’,
employees => <Alice Bob>),
Department.new(name => ‘Marketing’,
employees => <Margaret Terry Lawrence>),
Department.new(name => ‘Development’,
employees => <Matt Fred Steve Joe Alith Jie>)
);
my @print_info = @company».gen_print_info;
.say for @print_info;
class Dog {
method talk {‘bark’}
}
class TalkingDog is Dog {
method talk {‘Hello’}
}
my TalkingDog $td .= new;
say $td.talk;
say $td.?talk;
say $td.*talk;
say $td.+talk;
say $td.*caculate_pi;
say $td.caculate_pi;
say $td.?caculate_pi;
say $td.+caculate_pi;
class Dog {
has $.name is rw;
has $.color;
method kugo {
say "hello ",$.name;
}
}
my $pet = Dog.new(
name => 'Spot', color => 'Black'
);
$pet.kugo();
$pet.name = 'Fido';
$pet.kugo();
$pet.color = 'White';
class Employee {
subset Salary of Real where * > 0;
subset NonEmptyString of Str where * ~~ /\S/;
has NonEmptyString $.name is rw;
has NonEmptyString $.surname is rw;
has Salary $.salary is rw;
method gist {
return qq:to[END];
Name: {$.name}
Surname: {$.surname}
Salary: {$.salary}
END
}
}
my $employee = Employee.new();
given $employee {
.name = 'Sally';
.surname = 'Ride';
.salary = 200;
}
say $employee;
grammar Calculator {
token TOP { [ <add> | <sub> ] }
rule add { <num> '+' <num> }
rule sub { <num> '-' <num> }
token num { \d+ }
}
class Calculations {
method ws($) {}
method FALLBACK($token, $match-data) {
$match-data.make( [~] $match-data.chunks.map: {
$_.value.?made // $_.value;
} );
}
}
say Calculator.parse('2 + 3', actions => Calculations).made;
class Journey {
has $.origin;
has $.destination;
has @!travellers;
has $.notes is rw;
method add_traveller($name) {
if $name ne any(@!travellers) {
push @!travellers, $name;
} else {
warn "$name is already going on the journey!";
}
}
method describe() {
"From $!origin to $!destination";
}
method !do-something-private($x) {
($x + 120)*0.88;
}
method price($x) {
self!do-something-private(2*$x);
}
}
my $vacation = Journey.new(
origin => 'China',
destination => 'Sweden',
notes => 'Pack hiking'
);
say $vacation.origin;
$vacation.notes = 'Pack hiking gear and sunglasses!';
say $vacation.notes;
$vacation.add_traveller('Larry Wall');
say $vacation.price(40);
$vacation.add_traveller('Larry Wall');
class Journey {
has $.origin;
has $.destination;
has @!travellers;
has $.notes is rw;
method add_traveller($name) {
if $name ne any(@!travellers) {
push @!travellers, $name;
}
else {
warn "$name is already going on the journey!";
}
}
method describe() {
"From $!origin to $!destination"
}
}
class Point {
has $.x;
has $.y = 2 * $!x;
}
my $p = Point.new( x => 1, y => 2);
say "x: ", $p.x;
say "y: ", $p.y;
my $p2 = Point.new( x => 5 );
say "x: ", $p2.x;
say "y: ", $p2.y;
class Point {
has $.x;
has $!y;
method print() {
say self.x();
say $!y;
}
}
my $point = Point.new(x => 10, y => 20);
$point.print;
class Journey {
has $.origin;
has $.destination;
has @!travellers;
has $.notes;
}
my $j = Journey.new(
origin => 'Sweden',
destination => 'China',
notes => 'Be careful your money!'
);
say $j.origin;
say $j.destination;
say $j.notes;
$j.notes = 'gun nima dan';
say $j.notes;
class Journey {
has $.origin;
has $.destination;
has @!travellers;
has $.notes is rw;
}
my $vacation = Journey.new(
origin => 'Sweden',
destination => 'Switzerland',
notes => 'Pack hiking gear!'
);
say $vacation.origin;
$vacation.notes = 'Pack hiking gear and sunglasses!';
say $vacation.notes;
class Paper { }
class Scissor { }
class Stone { }
multi win(Paper $a, Stone $b) { 1 }
multi win(Scissor $a, Paper $b) { 1 }
multi win(Stone $a, Scissor $b) { 1 }
multi win(Any $a, Any $b) { 0 }
say win(Paper.new, Scissor.new);
say win(Stone.new, Stone.new);
say win(Paper.new, Stone.new);
class Point2D {
has $.x;
has $.y;
submethod BUILD(:$!x, :$!y) {
say "Initalizing Point2D";
}
}
class InvertiblePoint2D is Point2D {
submethod BUILD() {
say "Initilizing InvertiblePoint2D";
}
method invert {
self.new(x => - $.x, y => - $.y);
}
}
say InvertiblePoint2D.new( x => 1, y => 2);
class Cat {
has $.fullname;
has $.nickname;
submethod BUILD(:$!fullname, :$!nickname) {
say "造了一只猫, 它的全名是 $!fullname, 它的昵称是 $!nickname";
}
}
Cat.new(fullname => 'Camelia', nickname => 'Rakudo Star');
class A {
has $.value = 42;
method TWEAK(:$value = 0) {
$!value = 666 if $value == $!value;
}
}
dd A.new;
dd A.new(value => 77);
dd A.new(value => 42);
class C {
multi method f(::?CLASS:U:){say "class method"}
multi method f(::?CLASS:D:){say "object method"}
}
C.f;
C.new.f;
class B {
has $.name;
submethod BUILD(:$!name) {
say "调用了 B 的 BUILD, 我叫 $!name"
}
}
class C is B {
has $.nickname;
submethod BUILD(:$!nickname, :$name) {
say "调用了 C 的 BUILD, 我叫 $!nickname, 我爸爸是 $name"
}
method new(:$nickname) {
self.bless(nickname => 'Camelia', name => 'Lucy');
}
}
my $c = C.new(nickname => 'HANMEIMEI');
class WordCount {
has Int %.words is default(0);
method new($string) {
my Int %words;
for $string.split(/\s+/) -> $word {
%words{$word}++;
}
self.bless(:%words)
}
method gist {
%.words.map({.value ~ " " ~ .key}).join("\n")
}
}
my $word-count = WordCount.new('the boy jumped over the dog');
say $word-count;