diff --git a/Greet.pm b/Greet.pm new file mode 100644 index 0000000..80bc8a8 --- /dev/null +++ b/Greet.pm @@ -0,0 +1,5 @@ +unit module Greet; + +sub hey($name) is export { + say "Hey, $name!"; +} diff --git a/Math.pm b/Math.pm new file mode 100644 index 0000000..e159f77 --- /dev/null +++ b/Math.pm @@ -0,0 +1,5 @@ +unit module Math; + +our sub sum(*@a) { + return [+] @a; +} diff --git a/N.pm b/N.pm new file mode 100644 index 0000000..82f5b09 --- /dev/null +++ b/N.pm @@ -0,0 +1,5 @@ +unit module N; + +our sub n() { + say "N::n()"; +} diff --git a/arr1.pl b/arr1.pl new file mode 100644 index 0000000..0a373d2 --- /dev/null +++ b/arr1.pl @@ -0,0 +1,4 @@ +my @a = 1, 2, 3, 5, 7, 11; +say @a.Int; # array length +say @a.Str; # space-separated values + diff --git a/arr2.pl b/arr2.pl new file mode 100644 index 0000000..acb7778 --- /dev/null +++ b/arr2.pl @@ -0,0 +1,4 @@ +my @a = 1, 2, 3, 5, 7, 11; + +say @a; +say "This is @a: {@a}"; diff --git a/bool1.pl b/bool1.pl new file mode 100644 index 0000000..0f5921f --- /dev/null +++ b/bool1.pl @@ -0,0 +1,7 @@ +my $b = Bool::True; +$b--; +say $b; # prints False + +$b = Bool::False; +$b++; +say $b; # True diff --git a/bool2.pl b/bool2.pl new file mode 100644 index 0000000..e222ca3 --- /dev/null +++ b/bool2.pl @@ -0,0 +1,7 @@ +say 42.Bool; # True + +my $pi = 3.14; +say $pi.Bool; # True + +say 0.Bool; # False +say "00".Bool; # True diff --git a/bool3.pl b/bool3.pl new file mode 100644 index 0000000..46fb5e2 --- /dev/null +++ b/bool3.pl @@ -0,0 +1 @@ +say Bool::True.Int; # 1 diff --git a/dot-say.pl b/dot-say.pl new file mode 100644 index 0000000..37dd8f9 --- /dev/null +++ b/dot-say.pl @@ -0,0 +1,5 @@ +my @a = 10, 20, 30; +.say for @a; +.say() for @a; +$_.say for @a; +$_.say() for @a; diff --git a/ex1.pl b/ex1.pl new file mode 100644 index 0000000..fd98b52 --- /dev/null +++ b/ex1.pl @@ -0,0 +1,7 @@ +my @squares = 0, 1, 4, 9, 14, 25; +say @squares[3]; # This prints the 4th element, thus 9 + +my %capitals = + 'France' => 'Paris', + 'Germany' => 'Berlin'; +say %capitals{'Germany'}; diff --git a/ex10.pl b/ex10.pl new file mode 100644 index 0000000..2fbb049 --- /dev/null +++ b/ex10.pl @@ -0,0 +1 @@ +.say for @*ARGS; diff --git a/ex11.pl b/ex11.pl new file mode 100644 index 0000000..73ad4e4 --- /dev/null +++ b/ex11.pl @@ -0,0 +1,4 @@ +for @*ARGS { + $_.say; +} + diff --git a/ex12.pl b/ex12.pl new file mode 100644 index 0000000..9c0a755 --- /dev/null +++ b/ex12.pl @@ -0,0 +1,3 @@ +for @*ARGS { + .say if /\d/; +} diff --git a/ex13.pl b/ex13.pl new file mode 100644 index 0000000..30a26f2 --- /dev/null +++ b/ex13.pl @@ -0,0 +1,4 @@ +for @*ARGS { + $_.say if $_ ~~ /\d/; +} + diff --git a/ex14.pl b/ex14.pl new file mode 100644 index 0000000..2a30b9b --- /dev/null +++ b/ex14.pl @@ -0,0 +1,5 @@ +"Perl’s Birthday: 18 December 1987" ~~ + /(\d+)\s(\D+)\s(\d+)/; +say $/.Str; +say $/[$_] for 0..2; + diff --git a/ex2.pl b/ex2.pl new file mode 100644 index 0000000..dbecb5b --- /dev/null +++ b/ex2.pl @@ -0,0 +1,5 @@ +my %length-abbrs = + :m('meter'), + :km('kilometre'), + :cm('centimetre'); +say %length-abbrs; # prints kilometre diff --git a/ex3.pl b/ex3.pl new file mode 100644 index 0000000..f6607ee --- /dev/null +++ b/ex3.pl @@ -0,0 +1,8 @@ +my $hello-world = "Hello, World"; +say $hello-world; + +my $don't = "Isn’t it a Hello?"; +say $don't; + +my $привет = "A Cyrillic Hi!"; +say $привет; diff --git a/ex4.pl b/ex4.pl new file mode 100644 index 0000000..750c0d5 --- /dev/null +++ b/ex4.pl @@ -0,0 +1,6 @@ +my $scalar = 42; +my $hello-world = "Hello, World"; + +say $scalar.WHAT; +say $hello-world.WHAT; + diff --git a/ex5.pl b/ex5.pl new file mode 100644 index 0000000..814f28d --- /dev/null +++ b/ex5.pl @@ -0,0 +1,5 @@ +my @list = 10, 20, 30; +my @squares = 0, 1, 4, 9, 14, 25; + +say @list.WHAT; +say @squares.WHAT; diff --git a/ex6.pl b/ex6.pl new file mode 100644 index 0000000..2397abe --- /dev/null +++ b/ex6.pl @@ -0,0 +1,6 @@ +my %hash = 'Language' => 'Perl'; +my %capitals = 'France' => 'Paris'; + +say %hash.WHAT; +say %capitals.WHAT; + diff --git a/ex7.pl b/ex7.pl new file mode 100644 index 0000000..7de3349 --- /dev/null +++ b/ex7.pl @@ -0,0 +1,2 @@ +my $value = 42; +say "OK" if $value.WHAT === Int; diff --git a/ex8.pl b/ex8.pl new file mode 100644 index 0000000..5f27d9f --- /dev/null +++ b/ex8.pl @@ -0,0 +1,2 @@ +my $value = 42; +say "OK" if $value.isa(Int); diff --git a/ex9.pl b/ex9.pl new file mode 100644 index 0000000..237314c --- /dev/null +++ b/ex9.pl @@ -0,0 +1,3 @@ +my $scalar = 42; +say $scalar.VAR.name; + diff --git a/hash.pl b/hash.pl new file mode 100644 index 0000000..ea1ed59 --- /dev/null +++ b/hash.pl @@ -0,0 +1,5 @@ +my %hash = + 'Language' => 'Perl', + 'Version' => '6'; +say %hash; + diff --git a/hash1.pl b/hash1.pl new file mode 100644 index 0000000..47fde53 --- /dev/null +++ b/hash1.pl @@ -0,0 +1,8 @@ +my %hash = + Language => 'Perl', + Version => 6; + +say %hash.elems; # number of pairs in the hash +say %hash.keys; # the list of the keys +say %hash.values; # the list of the values + diff --git a/hash2.pl b/hash2.pl new file mode 100644 index 0000000..fb4ea21 --- /dev/null +++ b/hash2.pl @@ -0,0 +1,8 @@ +my %hash = + Language => 'Perl', + Version => 6; + +for %hash.pairs { + say $_.key; + say $_.value; +} diff --git a/hash3.pl b/hash3.pl new file mode 100644 index 0000000..97c11c8 --- /dev/null +++ b/hash3.pl @@ -0,0 +1,8 @@ +my %hash = + Language => 'Perl', + Version => 6; + +for %hash.invert { + say $_.key; + say $_.value; +} diff --git a/hash4.pl b/hash4.pl new file mode 100644 index 0000000..d11c173 --- /dev/null +++ b/hash4.pl @@ -0,0 +1,5 @@ +my %hash = + Language => 'Perl', + Version => 6; + +say %hash.kv; diff --git a/hello.pl b/hello.pl new file mode 100644 index 0000000..3278d42 --- /dev/null +++ b/hello.pl @@ -0,0 +1 @@ +say "Hello, Perl 6!"; diff --git a/int1.pl b/int1.pl new file mode 100644 index 0000000..25cdc21 --- /dev/null +++ b/int1.pl @@ -0,0 +1,3 @@ +my Int $x = + 12389147319583948275874801735817503285431532; +say $x; diff --git a/int2.pl b/int2.pl new file mode 100644 index 0000000..3bb984b --- /dev/null +++ b/int2.pl @@ -0,0 +1 @@ +say :16 diff --git a/int3.pl b/int3.pl new file mode 100644 index 0000000..0d4ecce --- /dev/null +++ b/int3.pl @@ -0,0 +1,2 @@ +my Int $x = 735_817_503_285_431_532; +say $x; diff --git a/int4.pl b/int4.pl new file mode 100644 index 0000000..6f28c6b --- /dev/null +++ b/int4.pl @@ -0,0 +1,8 @@ +my Int $a = 65; +say $a.chr; # A + +my Int $i = 17; +say $i.is-prime; # True + +say 42.is-prime; # False + diff --git a/interp.pl b/interp.pl new file mode 100644 index 0000000..46e09b6 --- /dev/null +++ b/interp.pl @@ -0,0 +1,2 @@ +my $x = 42; +say "this is $x" diff --git a/list.pl b/list.pl new file mode 100644 index 0000000..5c22e26 --- /dev/null +++ b/list.pl @@ -0,0 +1,2 @@ +my @list = (10, 20, 30); +say @list; diff --git a/list1.pl b/list1.pl new file mode 100644 index 0000000..616488b --- /dev/null +++ b/list1.pl @@ -0,0 +1,5 @@ +my @list1 = <10 20 30>; +say @list1; + +my @list2 = 10, 20, 30; +say @list2; diff --git a/mod1.pl b/mod1.pl new file mode 100644 index 0000000..3bdba86 --- /dev/null +++ b/mod1.pl @@ -0,0 +1,5 @@ +unit module X; + +sub x() { + say "X::x()"; +} diff --git a/mod2.pl b/mod2.pl new file mode 100644 index 0000000..276e4c5 --- /dev/null +++ b/mod2.pl @@ -0,0 +1,5 @@ +module X { + sub x() { + say "X::x()"; + } +} diff --git a/mod3.pl b/mod3.pl new file mode 100644 index 0000000..3475e0f --- /dev/null +++ b/mod3.pl @@ -0,0 +1,5 @@ +unit module X; + +sub x() is export { + say "X::x()"; +} diff --git a/mod4.pl b/mod4.pl new file mode 100644 index 0000000..e5f1951 --- /dev/null +++ b/mod4.pl @@ -0,0 +1,3 @@ +use Greet; + +hey("you"); # Hey, you! diff --git a/mod5.pl b/mod5.pl new file mode 100644 index 0000000..6fe1596 --- /dev/null +++ b/mod5.pl @@ -0,0 +1,5 @@ +use Greet; +use Greet::Polite; + +hey("you"); # a sub from Greet +hello("Mr. X"); # from Greet::Polite diff --git a/mod6.pl b/mod6.pl new file mode 100644 index 0000000..6c998fe --- /dev/null +++ b/mod6.pl @@ -0,0 +1,9 @@ +my module M { + sub f($x) is export { + return $x; + } +} + +import M; + +say f(42); diff --git a/mod7.pl b/mod7.pl new file mode 100644 index 0000000..07d78ab --- /dev/null +++ b/mod7.pl @@ -0,0 +1,8 @@ +my module M { + sub f($x) is export { + return $x; + } +} +say f(1); +import M; +say f(2); diff --git a/mod8.pl b/mod8.pl new file mode 100644 index 0000000..4c279b1 --- /dev/null +++ b/mod8.pl @@ -0,0 +1,3 @@ +need N; + +N::n(); diff --git a/mod9.pl b/mod9.pl new file mode 100644 index 0000000..679b852 --- /dev/null +++ b/mod9.pl @@ -0,0 +1,3 @@ +require Math; + +say Math::sum(24..42); # 627 diff --git a/op1.pl b/op1.pl new file mode 100644 index 0000000..dd4c3e7 --- /dev/null +++ b/op1.pl @@ -0,0 +1,3 @@ +say !True; # False +say !(1 == 2); # True + diff --git a/op10.pl b/op10.pl new file mode 100644 index 0000000..2c671e0 --- /dev/null +++ b/op10.pl @@ -0,0 +1,3 @@ +my $x = 10; +my $y = +^$x; +say $y; # -11 (but not -10) diff --git a/op11.pl b/op11.pl new file mode 100644 index 0000000..44b6899 --- /dev/null +++ b/op11.pl @@ -0,0 +1,4 @@ +my $x = 10; +my $y = ?^$x; +say $y; # False +say $y.WHAT; # (Bool) diff --git a/op12.pl b/op12.pl new file mode 100644 index 0000000..c8b6542 --- /dev/null +++ b/op12.pl @@ -0,0 +1 @@ +.print for ^5; # 01234 diff --git a/op13.pl b/op13.pl new file mode 100644 index 0000000..c0a5656 --- /dev/null +++ b/op13.pl @@ -0,0 +1 @@ +.print for 0..4; # 01234 diff --git a/op14.pl b/op14.pl new file mode 100644 index 0000000..6c7cda0 --- /dev/null +++ b/op14.pl @@ -0,0 +1,6 @@ +sub sum($a, $b) { + $a + $b +} + +my @data = (10, 20); +say sum(|@data); # 30 diff --git a/op15.pl b/op15.pl new file mode 100644 index 0000000..a9e295c --- /dev/null +++ b/op15.pl @@ -0,0 +1,6 @@ +my $x = 'x'; +{ + temp $x = 'y'; + say $x; # y +} +say $x; # x diff --git a/op16.pl b/op16.pl new file mode 100644 index 0000000..b7bce23 --- /dev/null +++ b/op16.pl @@ -0,0 +1,6 @@ +my $var = 'a'; +try { + let $var = 'b'; + die; +} +say $var; # a diff --git a/op17.pl b/op17.pl new file mode 100644 index 0000000..67f79fd --- /dev/null +++ b/op17.pl @@ -0,0 +1,3 @@ +my $x = 42; +say $x++; # 42 +say $x; # 43 diff --git a/op18.pl b/op18.pl new file mode 100644 index 0000000..399b845 --- /dev/null +++ b/op18.pl @@ -0,0 +1,4 @@ +my $filename = 'file01.txt'; +for 1..10 { + say $filename++; +} diff --git a/op19.pl b/op19.pl new file mode 100644 index 0000000..495c0f7 --- /dev/null +++ b/op19.pl @@ -0,0 +1,9 @@ +say "0.0".Numeric; # 0 +say 42.Bool; # True + +class C { + method m() {say "m()"} +} +my $c = C.new; +$c.m(); # m() + diff --git a/op2.pl b/op2.pl new file mode 100644 index 0000000..58ce122 --- /dev/null +++ b/op2.pl @@ -0,0 +1,2 @@ +my Str $price = '4' ~ '2'; +say -$price; # -42 diff --git a/op20.pl b/op20.pl new file mode 100644 index 0000000..a16a2e8 --- /dev/null +++ b/op20.pl @@ -0,0 +1,13 @@ +class D { } + +class C { + method m() { + return D.new; + } +} + +my $o = C.new; +say $o.WHAT; # (C) + +$o .= m(); +say $o.WHAT; # (D) diff --git a/op21.pl b/op21.pl new file mode 100644 index 0000000..00e6d78 --- /dev/null +++ b/op21.pl @@ -0,0 +1,3 @@ +my Int $i; +say $i.^methods(); +say $i.HOW.methods($i); diff --git a/op22.pl b/op22.pl new file mode 100644 index 0000000..1776637 --- /dev/null +++ b/op22.pl @@ -0,0 +1,7 @@ +class C { + method m() {'m'} +} + +my $c = C.new(); +say $c.?m(); # m +say $c.?n(); # Nil diff --git a/op23.pl b/op23.pl new file mode 100644 index 0000000..33f94c9 --- /dev/null +++ b/op23.pl @@ -0,0 +1,10 @@ +class A { + method m($x) {"A::m($x)"} +} +class B is A { + method m($x) {"B::m($x)"} +} + +my $o = B.new; +my @a = $o.+m(7); +say @a; # Prints [B::m(7) A::m(7)] diff --git a/op24.pl b/op24.pl new file mode 100644 index 0000000..2c9d2b9 --- /dev/null +++ b/op24.pl @@ -0,0 +1,2 @@ +say 10 div 3; # 3 +say -10 div 3; # 4 diff --git a/op25.pl b/op25.pl new file mode 100644 index 0000000..724ff8a --- /dev/null +++ b/op25.pl @@ -0,0 +1,2 @@ +say 10 % 3; # 1 +say 10 mod 3; # 1 diff --git a/op26.pl b/op26.pl new file mode 100644 index 0000000..2b2a9e7 --- /dev/null +++ b/op26.pl @@ -0,0 +1 @@ +say 10 % "3"; # 1 diff --git a/op27.pl b/op27.pl new file mode 100644 index 0000000..4bbb7cf --- /dev/null +++ b/op27.pl @@ -0,0 +1 @@ +say 10 mod "3"; diff --git a/op28.pl b/op28.pl new file mode 100644 index 0000000..508327a --- /dev/null +++ b/op28.pl @@ -0,0 +1 @@ +say 10 div "3"; diff --git a/op29.pl b/op29.pl new file mode 100644 index 0000000..c82d61e --- /dev/null +++ b/op29.pl @@ -0,0 +1,2 @@ +say 10 mod +"3"; # 1 +say 10 mod "3".Int; # 1 diff --git a/op3.pl b/op3.pl new file mode 100644 index 0000000..5a12796 --- /dev/null +++ b/op3.pl @@ -0,0 +1 @@ +say ?42; # True diff --git a/op30.pl b/op30.pl new file mode 100644 index 0000000..db9fc56 --- /dev/null +++ b/op30.pl @@ -0,0 +1,3 @@ +say 10 %% 3; # False +say 12 %% 3; # True + diff --git a/op31.pl b/op31.pl new file mode 100644 index 0000000..2686ecf --- /dev/null +++ b/op31.pl @@ -0,0 +1,2 @@ +say 8 +< 2; # 32 +say 1024 +> 8; # 4 diff --git a/op32.pl b/op32.pl new file mode 100644 index 0000000..1594a35 --- /dev/null +++ b/op32.pl @@ -0,0 +1 @@ +say 50 gcd 15; # 5 diff --git a/op33.pl b/op33.pl new file mode 100644 index 0000000..1a218c7 --- /dev/null +++ b/op33.pl @@ -0,0 +1 @@ +say 1043 lcm 14; # 2086 diff --git a/op34.pl b/op34.pl new file mode 100644 index 0000000..2dd35d6 --- /dev/null +++ b/op34.pl @@ -0,0 +1 @@ +say "a" ~ "b"; # ab diff --git a/op35.pl b/op35.pl new file mode 100644 index 0000000..4aac00a --- /dev/null +++ b/op35.pl @@ -0,0 +1,3 @@ +say "N" ~ 1; # N1 +say 4 ~ 2; # 42 + diff --git a/op36.pl b/op36.pl new file mode 100644 index 0000000..6214a56 --- /dev/null +++ b/op36.pl @@ -0,0 +1 @@ +say "A" x 5; # AAAAA diff --git a/op37.pl b/op37.pl new file mode 100644 index 0000000..5a6e480 --- /dev/null +++ b/op37.pl @@ -0,0 +1 @@ +say 0 x 5; # 0000 diff --git a/op38.pl b/op38.pl new file mode 100644 index 0000000..17a6ca8 --- /dev/null +++ b/op38.pl @@ -0,0 +1,4 @@ +say "a" leg "b"; # Less +say "abc" leg "b"; # Less +say "bc" leg "b"; # More +say "abc" leg "ABC".lc; # Same diff --git a/op39.pl b/op39.pl new file mode 100644 index 0000000..fb1270c --- /dev/null +++ b/op39.pl @@ -0,0 +1,2 @@ +say 42 leg "+42"; # More +say 42 leg "42"; # Same diff --git a/op4.pl b/op4.pl new file mode 100644 index 0000000..0a6b8c1 --- /dev/null +++ b/op4.pl @@ -0,0 +1,3 @@ +say so 42; # True +say so True; # True +say so 0.0; # False diff --git a/op40.pl b/op40.pl new file mode 100644 index 0000000..95190d0 --- /dev/null +++ b/op40.pl @@ -0,0 +1,13 @@ +say 2 cmp 2; # Same +say 2 cmp 2.0; # Same +say 1 cmp 2; # Less +say 2 cmp 1; # More + +say "a" cmp "b"; # Less +say "abc" cmp "b"; # Less +say "bc" cmp "b"; # More +say "abc" cmp "ABC".lc; # Same + +my %a = (a => 1); +my %b = (a => 1); +say %a cmp %b; # Same diff --git a/op41.pl b/op41.pl new file mode 100644 index 0000000..fae17cd --- /dev/null +++ b/op41.pl @@ -0,0 +1,3 @@ +say "+42" cmp +42; # Less +say ~42 cmp +42; # Same + diff --git a/op42.pl b/op42.pl new file mode 100644 index 0000000..dc9fa5f --- /dev/null +++ b/op42.pl @@ -0,0 +1,4 @@ +say 10 before 2; # False +say '10' before '2'; # True +say 10 before 20; # True +say '10' before '20'; # True diff --git a/op43.pl b/op43.pl new file mode 100644 index 0000000..532e565 --- /dev/null +++ b/op43.pl @@ -0,0 +1,4 @@ +my $x = 3; +my $y = 3; +say $x eqv $y; # True + diff --git a/op44.pl b/op44.pl new file mode 100644 index 0000000..bb29251 --- /dev/null +++ b/op44.pl @@ -0,0 +1,4 @@ +my @a = (3, 4); +my @b = (3, 4, 5); +@b.pop; +say @a eqv @b; # True diff --git a/op45.pl b/op45.pl new file mode 100644 index 0000000..ffb833e --- /dev/null +++ b/op45.pl @@ -0,0 +1,2 @@ +say 42 eqv 42.0; # False +say 42 eqv "42"; # False diff --git a/op46.pl b/op46.pl new file mode 100644 index 0000000..0830b79 --- /dev/null +++ b/op46.pl @@ -0,0 +1,2 @@ +say 42 eqv 84/2; # False +say 42 eqv (84/2).Int; # True diff --git a/op47.pl b/op47.pl new file mode 100644 index 0000000..26f2e40 --- /dev/null +++ b/op47.pl @@ -0,0 +1,15 @@ +class I { } + +# Three different instances +my $i = I.new; +my $ii = I.new; +my $iii = I.new; + +my @a = ($i, $ii, $iii); +for @a -> $a { + for @a -> $b { + say $a === $b; + # Prints True only when $a and $b are pointing + # to the same element of the @a array. + } +} diff --git a/op48.pl b/op48.pl new file mode 100644 index 0000000..da6e306 --- /dev/null +++ b/op48.pl @@ -0,0 +1,5 @@ +my $x = 42; +my $y := $x; + +say $x =:= $y; # True +say $y =:= $x; # True diff --git a/op49.pl b/op49.pl new file mode 100644 index 0000000..717855b --- /dev/null +++ b/op49.pl @@ -0,0 +1,2 @@ +say 42 ~~ 42.0; # True +say 42 ~~ "42"; # True diff --git a/op5.pl b/op5.pl new file mode 100644 index 0000000..f743462 --- /dev/null +++ b/op5.pl @@ -0,0 +1,3 @@ +my Str $a = ~42; +say $a.WHAT; # (Str) + diff --git a/op50.pl b/op50.pl new file mode 100644 index 0000000..8e8791d --- /dev/null +++ b/op50.pl @@ -0,0 +1,2 @@ +say "42.0" ~~ 42; # True +say 42 ~~ "42.0"; # False diff --git a/op51.pl b/op51.pl new file mode 100644 index 0000000..4f6c013 --- /dev/null +++ b/op51.pl @@ -0,0 +1,2 @@ +say 42.ACCEPTS("42.0"); # True +say "42.0".ACCEPTS(42); # False diff --git a/op52.pl b/op52.pl new file mode 100644 index 0000000..2c960d5 --- /dev/null +++ b/op52.pl @@ -0,0 +1 @@ +say((1, -1) xx 2); # 1 -1 1 -1 diff --git a/op53.pl b/op53.pl new file mode 100644 index 0000000..f198356 --- /dev/null +++ b/op53.pl @@ -0,0 +1,4 @@ +my @a = 1..10; +my @b = 11..20; +my @c = @a Z @b; +say @c; diff --git a/op54.pl b/op54.pl new file mode 100644 index 0000000..6860b46 --- /dev/null +++ b/op54.pl @@ -0,0 +1,4 @@ +my @a = 1..10; +my @b = 11..20; +my @c = @a X @b; +say @c; diff --git a/op55.pl b/op55.pl new file mode 100644 index 0000000..e5e6986 --- /dev/null +++ b/op55.pl @@ -0,0 +1,2 @@ +my @list = 1 ... 10; +say @list; diff --git a/op56.pl b/op56.pl new file mode 100644 index 0000000..389a2b5 --- /dev/null +++ b/op56.pl @@ -0,0 +1,2 @@ +my @back = 10 ... 1; +say @back; diff --git a/op57.pl b/op57.pl new file mode 100644 index 0000000..4c50de8 --- /dev/null +++ b/op57.pl @@ -0,0 +1,2 @@ +my @long = 1 ... *; +say @long[$_] for 0..10; diff --git a/op58.pl b/op58.pl new file mode 100644 index 0000000..d8e08c7 --- /dev/null +++ b/op58.pl @@ -0,0 +1,3 @@ +say 10 && 0; # 0 +say 0 && 10; # 0 +say 12 && 3.14 && "" && "abc"; # empty string diff --git a/op59.pl b/op59.pl new file mode 100644 index 0000000..7d823b6 --- /dev/null +++ b/op59.pl @@ -0,0 +1,3 @@ +say 10 || 0; # 10 +say 0 || 10; # 10 + diff --git a/op6.pl b/op6.pl new file mode 100644 index 0000000..b7b2faf --- /dev/null +++ b/op6.pl @@ -0,0 +1,2 @@ +my $x = 41; +say ++$x; # 42 diff --git a/op60.pl b/op60.pl new file mode 100644 index 0000000..5a368b8 --- /dev/null +++ b/op60.pl @@ -0,0 +1,5 @@ +my $x; +my $y = 42; +my $z; +say $x // $y // $z; # 42 + diff --git a/op61.pl b/op61.pl new file mode 100644 index 0000000..bf1de49 --- /dev/null +++ b/op61.pl @@ -0,0 +1 @@ +say rand < 0.5 ?? 'Yes' !! 'No'; diff --git a/op62.pl b/op62.pl new file mode 100644 index 0000000..32f1f24 --- /dev/null +++ b/op62.pl @@ -0,0 +1,6 @@ +my $pair = alpha => "one"; + +my %data = jan => 31, feb => 28, mar => 31; + +say $pair; +say %data; diff --git a/op63.pl b/op63.pl new file mode 100644 index 0000000..8d3b68d --- /dev/null +++ b/op63.pl @@ -0,0 +1,2 @@ +my $what = (1, 2, 3); +say $what.WHAT; # (Parcel) diff --git a/op64.pl b/op64.pl new file mode 100644 index 0000000..2fc2572 --- /dev/null +++ b/op64.pl @@ -0,0 +1,9 @@ +class C { + method meth($x) { + say "meth($x)"; + } +} +my $o = C.new; +meth($o: 42); # The meth method of the $o object is called, + # it prints “meth(42)” +$o.meth(42); diff --git a/op7.pl b/op7.pl new file mode 100644 index 0000000..1fff60d --- /dev/null +++ b/op7.pl @@ -0,0 +1,2 @@ +my $a = 'a'; +say ++$a; # b diff --git a/op8.pl b/op8.pl new file mode 100644 index 0000000..e173a2d --- /dev/null +++ b/op8.pl @@ -0,0 +1,7 @@ +my $f = "file001.txt"; + +++$f; +say $f; # file002.txt + +++$f; +say $f; # file003.txt diff --git a/op9.pl b/op9.pl new file mode 100644 index 0000000..8979372 --- /dev/null +++ b/op9.pl @@ -0,0 +1,2 @@ +my $x = 42; +say --$x; # 41 diff --git a/plus1.pl b/plus1.pl new file mode 100644 index 0000000..afcb192 --- /dev/null +++ b/plus1.pl @@ -0,0 +1,4 @@ +my Str $price = '4' ~ '2'; +my Int $amount = +$price; +say $amount; # 42 +say $price.Numeric; # 42 diff --git a/plus_m.pl b/plus_m.pl new file mode 100644 index 0000000..88d996a --- /dev/null +++ b/plus_m.pl @@ -0,0 +1,10 @@ +class A { + method m($x) {"A::m($x)"} +} +class B is A { + method m($x) {"B::m($x)"} +} + +my $o = B.new; +my @a = $o.+m(7); +say @a; # Prints B::m(7) A::m(7) diff --git a/scalar.pl b/scalar.pl new file mode 100644 index 0000000..686be8d --- /dev/null +++ b/scalar.pl @@ -0,0 +1,3 @@ +my $scalar = 42; +say $scalar; + diff --git a/str1.pl b/str1.pl new file mode 100644 index 0000000..7b1c468 --- /dev/null +++ b/str1.pl @@ -0,0 +1,6 @@ +my $str = "My string"; + +say $str.lc; # my string +say $str.uc; # MY STRING + +say $str.index('t'); # 4 diff --git a/str2.pl b/str2.pl new file mode 100644 index 0000000..c3171a7 --- /dev/null +++ b/str2.pl @@ -0,0 +1 @@ +say "περλ 6".chars; # 6 diff --git a/str3.pl b/str3.pl new file mode 100644 index 0000000..cca4934 --- /dev/null +++ b/str3.pl @@ -0,0 +1,5 @@ +my $day = 25; +my $month = 'December'; +my $year = 2016; + +"Today is %02i %s %i\n".printf($day, $month, $year); diff --git a/sub1.pl b/sub1.pl new file mode 100644 index 0000000..15d4d29 --- /dev/null +++ b/sub1.pl @@ -0,0 +1,5 @@ +sub call-me { + say "I'm called" +} + +call-me; diff --git a/sub10.pl b/sub10.pl new file mode 100644 index 0000000..1b02202 --- /dev/null +++ b/sub10.pl @@ -0,0 +1,5 @@ +sub power(:val($base), :pow($exponent)) { + return $base ** $exponent; +} +say power(:val(5), :pow(2)); # 25 +say power(:pow(2), :val(5)); # 25 diff --git a/sub11.pl b/sub11.pl new file mode 100644 index 0000000..35ec687 --- /dev/null +++ b/sub11.pl @@ -0,0 +1,6 @@ +sub cute-output(@text, $before, $after) { + say $before ~ $_ ~ $after for @text; +} + +my @text = ; +cute-output(@text, '{', '}'); diff --git a/sub12.pl b/sub12.pl new file mode 100644 index 0000000..7c6d8bc --- /dev/null +++ b/sub12.pl @@ -0,0 +1,6 @@ +sub get-array(@a) { + say @a; +} + +get-array(1, 2, 3); # Error: Calling 'get-array' will + # never work with argument types (Int, Int, Int) diff --git a/sub13.pl b/sub13.pl new file mode 100644 index 0000000..be34c46 --- /dev/null +++ b/sub13.pl @@ -0,0 +1,5 @@ +sub get-array(*@a) { + say @a; +} + +get-array(1, 2, 3); # Good: 1 2 3 diff --git a/sub14.pl b/sub14.pl new file mode 100644 index 0000000..1380206 --- /dev/null +++ b/sub14.pl @@ -0,0 +1,8 @@ +sub get-scalars($a, $b, $c) { + say "$a and $b and $c"; +} + +my @a = <3 4 5>; +#get-scalars(@a); # Error: Calling 'get-scalars' will + # never work with argument types (Positional) +get-scalars(|@a); # 3 and 4 and 5 \ No newline at end of file diff --git a/sub15.pl b/sub15.pl new file mode 100644 index 0000000..88d3c2e --- /dev/null +++ b/sub15.pl @@ -0,0 +1,9 @@ +sub cube($x) { + sub square($x) { + return $x * $x; + } + + return $x * square($x); +} + +say cube(3); # 27 diff --git a/sub16.pl b/sub16.pl new file mode 100644 index 0000000..6963a31 --- /dev/null +++ b/sub16.pl @@ -0,0 +1 @@ +say sub ($x, $y) {$x ~ ' ' ~ $y}("Perl", 6); diff --git a/sub18.pl b/sub18.pl new file mode 100644 index 0000000..da79bea --- /dev/null +++ b/sub18.pl @@ -0,0 +1,9 @@ +sub seq($init) { + my $c = $init; + return {$c++}; +} +my $a = seq(1); + +say $a(); # 1 +say $a(); # 2 +say $a(); # 3 diff --git a/sub19.pl b/sub19.pl new file mode 100644 index 0000000..002003d --- /dev/null +++ b/sub19.pl @@ -0,0 +1,13 @@ + +sub seq($init) { + my $c = $init; + return {$c++}; +} +my $a = seq(1); +my $b = seq(42); + +say $a(); # 1 +say $a(); # 2 +say $b(); # 42 +say $a(); # 3 +say $b(); # 43 diff --git a/sub2.pl b/sub2.pl new file mode 100644 index 0000000..92ed9dc --- /dev/null +++ b/sub2.pl @@ -0,0 +1,5 @@ +sub cube($x) { + return $x ** 3; +} + +say cube(3); # 27 diff --git a/sub20.pl b/sub20.pl new file mode 100644 index 0000000..7663247 --- /dev/null +++ b/sub20.pl @@ -0,0 +1,12 @@ +sub seq($init) { + state $c = $init; + return {$c++}; +} +my $a = seq(1); +my $b = seq(42); + +say $a(); # 1 +say $a(); # 2 +say $b(); # 3 +say $a(); # 4 +say $b(); # 5 diff --git a/sub21.pl b/sub21.pl new file mode 100644 index 0000000..a95376b --- /dev/null +++ b/sub21.pl @@ -0,0 +1,16 @@ +sub alpha { + my $*var = 'Alpha'; + echo(); +} + +sub beta { + my $*var = 'Beta'; + echo(); +} + +sub echo() { + say $*var; +} + +alpha(); # Alpha +beta(); # Beta diff --git a/sub22.pl b/sub22.pl new file mode 100644 index 0000000..abfbbff --- /dev/null +++ b/sub22.pl @@ -0,0 +1,2 @@ +my $cube = -> $x {$x ** 3}; +say $cube(3); # 27 diff --git a/sub23.pl b/sub23.pl new file mode 100644 index 0000000..898de89 --- /dev/null +++ b/sub23.pl @@ -0,0 +1,3 @@ +for 1..10 -> $c { + say $c; +} diff --git a/sub24.pl b/sub24.pl new file mode 100644 index 0000000..e8c9fc6 --- /dev/null +++ b/sub24.pl @@ -0,0 +1,2 @@ +my $pow = -> $x, $p {$x ** $p}; +say $pow(2, 15); # 32768 diff --git a/sub25.pl b/sub25.pl new file mode 100644 index 0000000..a4c2800 --- /dev/null +++ b/sub25.pl @@ -0,0 +1,3 @@ +for 0..9 -> $i, $j { + say $i + $j; +} diff --git a/sub26.pl b/sub26.pl new file mode 100644 index 0000000..cd27022 --- /dev/null +++ b/sub26.pl @@ -0,0 +1,2 @@ +my $pow = {$^x ** $^y}; +say $pow(3, 4); # 81 diff --git a/sub27.pl b/sub27.pl new file mode 100644 index 0000000..36a6d8e --- /dev/null +++ b/sub27.pl @@ -0,0 +1,3 @@ +for 0..9 { + say "$^n2, $^n1"; +} diff --git a/sub28.pl b/sub28.pl new file mode 100644 index 0000000..0bbc392 --- /dev/null +++ b/sub28.pl @@ -0,0 +1,4 @@ +my $pow = {$:base ** $:exp}; +say $pow(:base(25), :exp(2)); # 625 +say $pow(:exp(2), :base(25)); # 625 + diff --git a/sub29.pl b/sub29.pl new file mode 100644 index 0000000..b7a2c18 --- /dev/null +++ b/sub29.pl @@ -0,0 +1,10 @@ +multi sub twice(Int $x) { + return $x * 2; +} + +multi sub twice(Str $s) { + return "$s, $s"; +} + +say twice(42); # 84 +say twice("hi"); # hi, hi diff --git a/sub3.pl b/sub3.pl new file mode 100644 index 0000000..1ea4e68 --- /dev/null +++ b/sub3.pl @@ -0,0 +1,6 @@ +sub min($x, $y) { + return $x < $y ?? $x !! $y; +} + +say min(-2, 2); +say min(42, 24); diff --git a/sub4.pl b/sub4.pl new file mode 100644 index 0000000..8230829 --- /dev/null +++ b/sub4.pl @@ -0,0 +1,8 @@ +sub inc($x is rw) { + $x++; + return $x; +} + +my $value = 42; +inc($value); +say $value; # 43 diff --git a/sub5.pl b/sub5.pl new file mode 100644 index 0000000..852e3cc --- /dev/null +++ b/sub5.pl @@ -0,0 +1,8 @@ +sub say-hi(Str $name) { + say "Hi, $name!"; +} +say-hi("Mr. X"); # OK + +# say-hi(123); # Calling 'say-hi' will never work with + # argument types (int) + # Expected: :(Str $name) diff --git a/sub6.pl b/sub6.pl new file mode 100644 index 0000000..cd789d7 --- /dev/null +++ b/sub6.pl @@ -0,0 +1,14 @@ +sub send-mail(Str $to, Str $bcc?) { + if defined $bcc { + # . . . + say "Sent to $to with a bcc to $bcc."; + } + else { + # . . . + say "Sent to $to."; + } +} + +send-mail('mail@example.com'); + +send-mail('mail@example.com', 'copy@example.com'); diff --git a/sub7.pl b/sub7.pl new file mode 100644 index 0000000..740d112 --- /dev/null +++ b/sub7.pl @@ -0,0 +1,8 @@ +sub i-live-in(Str $city = "Moscow") { + say "I live in $city."; +} + +i-live-in('Saint Petersburg'); + +i-live-in(); + diff --git a/sub8.pl b/sub8.pl new file mode 100644 index 0000000..6a5bd6a --- /dev/null +++ b/sub8.pl @@ -0,0 +1,7 @@ +sub to-pay($salary, $bonus = 100.rand) { + return ($salary + $bonus).floor; +} + +say to-pay(500, 50); # Always 550 net. +say to-pay(500); # Any number between 500 and 600. +say to-pay(500); # Same call but probably different output. diff --git a/sub9.pl b/sub9.pl new file mode 100644 index 0000000..271a24a --- /dev/null +++ b/sub9.pl @@ -0,0 +1,5 @@ +sub power(:$base, :$exponent) { + return $base ** $exponent; +} +say power(:base(2), :exponent(3)); # 8 +say power(:exponent(3), :base(2)); # 8 diff --git a/subset.pl b/subset.pl new file mode 100644 index 0000000..22f1d27 --- /dev/null +++ b/subset.pl @@ -0,0 +1,13 @@ +subset Odd of Int where {$^n % 2 == 1}; +subset Even of Int where {$^n % 2 == 0}; +multi sub testnum(Odd $x) { + say "$x is odd"; +} + +multi sub testnum(Even $x) { + say "$x is even"; +} + +for 1..4 -> $x { + testnum($x); +} diff --git a/try1.pl b/try1.pl new file mode 100644 index 0000000..e79f0c8 --- /dev/null +++ b/try1.pl @@ -0,0 +1,4 @@ +try { + say 42/0; +} +say $!; diff --git a/twigil-question.pl b/twigil-question.pl new file mode 100644 index 0000000..baaeb23 --- /dev/null +++ b/twigil-question.pl @@ -0,0 +1,4 @@ +say $?FILE; +say $?LINE; +say $?PACKAGE; +say $?TABSTOP; diff --git a/twigil-vars.pl b/twigil-vars.pl new file mode 100644 index 0000000..ea464e3 --- /dev/null +++ b/twigil-vars.pl @@ -0,0 +1,10 @@ +say $*PERL; +say $*PID; +say $*PROGRAM-NAME; +say $*EXECUTABLE; +say $*VM; +say $*DISTRO; +say $*KERNEL; +say $*CWD; +say $*TZ; +say %*ENV; diff --git a/var17.pl b/var17.pl new file mode 100644 index 0000000..5f669bc --- /dev/null +++ b/var17.pl @@ -0,0 +1,5 @@ +{ + my $x = 42; + say $x; # This is fine +} +# say $x; # This is not