From 2a8e20ca27c59347df58197f821b1eee2e913128 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sat, 28 Jun 2014 22:59:14 +0200 Subject: Basic Perl 6 LearnXinYminutes --- perl6.html.markdown | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 perl6.html.markdown (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown new file mode 100644 index 00000000..4ba76d6c --- /dev/null +++ b/perl6.html.markdown @@ -0,0 +1,121 @@ +--- +name: perl6 +category: language +language: perl6 +filename: learnperl6.pl +contributors: + - ["Nami-Doc", "http://github.com/Nami-Doc"] +--- + +Perl 6 is a highly capable, feature-rich programming language made for the upcoming hundred years. + +Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM and [the MoarVM](http://moarvm.com). + +```perl6 +# Single line comment start with a pound + +#`( + Multiline comments use #` and a quoting construct. (), [], {}, 「」, etc, will work. +) + +### Variables + +# In Perl 6, you declare a lexical variable using `my` + +# Perl 6 has 4 variable types : + +## - Scalars. They represent a single value. They start with a `$` + +my $str = 'String'; +my $str2 = "String"; # double quotes allow for interpolation + +# variable names can contain but not end with simple quotes and dashes, and can contain (and end with) underscores + +my $weird'variable-name_ = 5; + +## - Arrays. They represent multiple values. They start with `@` + +my @array = 1, 2, 3; +my @array = 'a', 'b', 'c'; +# equivalent to : +my @array = ; # similar to perl5's qw, or Ruby's %w + +say @array[2]; # Arrays are 0-indexed + +## - Hashes + +my %hash = 1 => 2, + 3 => 4; +my %hash = autoquoted => "key", + "some other" => "value", # trailing commas are okay + ; +my %hash = # you can also create a hash from an even-numbered array + +say %hash{'key1'}; # You can use {} to get the value from a key +say %hash; # if it's a string, you can actually use <> + +## - Subs (subroutines, or functions in most other languages). Stored in variable, they use `&` +sub say-hello { say "Hello, world" } + +# since you can omit parenthesis to call a function with no arguments, you need to use `&` also to capture `say-hello` +my &s = &say-hello; +my &other-s = sub { say "anonymous function !" } + +# `->`, lambda with arguments, and string interpolation +my &lambda = -> $argument { "The argument passed to this lambda is $argument" } + +### Control Flow Structures + +# You don't need to put parenthesis around the condition, but that also means you always have to use brackets (`{ }`) for their body : + +## Conditionals + +if True { + say "It's true !"; +} + +unless False { + say "It's not false !"; +} + +# if (true) say; # Won't work + +# `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching : +given "foo bar" { # given just puts its argument into `$_`, and `when` uses it. + when /foo/ { # smart matching a string with a regex returns true if it matches + say "Yay !"; + } + when $_.chars > 50 { # smart matching anything with True gives True, so you can also put "normal" conditionals + say "Quite a long string !"; + } +} + +## Looping constructs + +### - `loop` is an infinite loop if you don't pass it arguments, but can also be a c-style `for` : +loop { + say "This is an infinite loop !"; + last; # last breaks out of the loop, like the `break` keyword in other languages +} + +loop (my $i = 0; $i < 5; $i++) { + next if $i == 3; # `next` skips to the next iteration, like `continue` in other languages. + # Notice that you can also use postfix conditionals, loops, etc. + say "This is a C-style for loop !"; +} + +### - `for` - Foreaches an array +for @array -> $variable { + say "I've found $variable !"; +} + +# default variable is $_ +for array { + say "I've got $_"; +} + +# Note - the "lambda" `->` syntax isn't reserved to for : +if long-computation() -> $result { + say "The result is $result"; +} +``` -- cgit v1.2.3 From 4b9c50733d9a2b20ce09b8a42c36df47a0ea10c7 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sun, 29 Jun 2014 21:41:57 +0200 Subject: Moar operators --- perl6.html.markdown | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 2 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 4ba76d6c..7a8c7525 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -78,11 +78,12 @@ unless False { say "It's not false !"; } + # if (true) say; # Won't work # `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching : -given "foo bar" { # given just puts its argument into `$_`, and `when` uses it. - when /foo/ { # smart matching a string with a regex returns true if it matches +given "foo bar" { # given just puts its argument into `$_`, and `when` uses it using the "smart matching" operator. + when /foo/ { # you'll read about the smart-matching operator below say "Yay !"; } when $_.chars > 50 { # smart matching anything with True gives True, so you can also put "normal" conditionals @@ -118,4 +119,85 @@ for array { if long-computation() -> $result { say "The result is $result"; } + + + +# Operators + +## Since Perl languages are very much operator-based languages +## Perl 6 operators are actually just funny-looking subroutines, in syntactic categories, +## like infix:<+> (addition) or prefix: (bool not) + +## The categories are : +### - "prefix" : before (like `!` in `!True`). +### "postfix" : after (like `++` in `$a++`). +### "infix" : in between (like `*` in `4 * 3`). +### "circumfix" : around (like `[`-`]` in `[1, 2]`). +### "post-circumfix" : around, after another term (like `{`-`}` in `%hash{'key'}`) + +## The precedence list can be found here : http://perlcabal.org/syn/S03.html#Operator_precedence +## But first, we need a little explanation about associativity : + +### Binary operators: +$a ! $b ! $c; # with a left-associative `!`, this is `($a ! $b) ! $c` +$a ! $b ! $c; # with a right-associative `!`, this is `$a ! ($b ! $c)` +$a ! $b ! $c; # with a non-associative `!`, this is illegal +$a ! $b ! $c; # with a chain-associative `!`, this is `($a ! $b) and ($b ! $c)` +$a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` + +### Unary operators: +!$a! # with left-associative `!`, this is `(!$a)!` +!$a! # with right-associative `!`, this is `!($a!)` +!$a! # with non-associative `!`, this is illegal + +## Alright, you're set to go ! + +## * Equality Checking + +### - `==` is numeric comparison +3 == 4; # False +3 != 4; # True + +### - `eq` is string comparison +'a' eq 'b'; +'a' ne 'b'; # not equal +'a' !eq 'b'; # same as above + +### - `eqv` is canonical equivalence +(1, 2) eqv (1, 3); + +### - `~~` is smart matching +### for a complete combinations list, use this table : http://perlcabal.org/syn/S03.html#Smart_matching +'a' ~~ /a/; # true if matches regexp +'key' ~~ %hash; # true if key exists in hash +$arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an argument, returns True +1 ~~ Int; # "is of type" + +### - `===` is value identity and uses `.WHICH` on the objects to compare them +### - `=:=` is container identity and uses `VAR()` on the objects to compare them + +### You also, of course, have `<`, `<=`, `>`, `>=`. +### Their string equivalent are also avaiable : `lt`, `le`, `gt`, `ge`. +3 > 4; + +## * Sort comparison +### They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). +1 <=> 4; # sort comparison for numerics +'a' leg 'b'; # sort comparison for string +$obj eqv $obj2; # sort comparison using eqv semantics + +## * Generic ordering +3 before 4; # True +'b' after 'a'; # True + +## * Range constructors +3 .. 7; # 3 to 7, both included +### `^` on either side them exclusive on that side : +3 ^..^ 7; # 3 to 7, not included (basically `4 .. 6`) + +# * And, Or + +## Short-circuit (and tight) +$a && $b && $c; # returns the first argument that evaluates to False, or the last argument +$a || $b; ``` -- cgit v1.2.3 From 7db96aafe39ff4dab601a73a871c02a9841cf6b0 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Thu, 10 Jul 2014 22:23:56 +0200 Subject: switcheroo operators --- perl6.html.markdown | 67 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 28 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 7a8c7525..f86c3d64 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -130,25 +130,12 @@ if long-computation() -> $result { ## The categories are : ### - "prefix" : before (like `!` in `!True`). -### "postfix" : after (like `++` in `$a++`). -### "infix" : in between (like `*` in `4 * 3`). -### "circumfix" : around (like `[`-`]` in `[1, 2]`). -### "post-circumfix" : around, after another term (like `{`-`}` in `%hash{'key'}`) +### - "postfix" : after (like `++` in `$a++`). +### - "infix" : in between (like `*` in `4 * 3`). +### - "circumfix" : around (like `[`-`]` in `[1, 2]`). +### - "post-circumfix" : around, after another term (like `{`-`}` in `%hash{'key'}`) -## The precedence list can be found here : http://perlcabal.org/syn/S03.html#Operator_precedence -## But first, we need a little explanation about associativity : - -### Binary operators: -$a ! $b ! $c; # with a left-associative `!`, this is `($a ! $b) ! $c` -$a ! $b ! $c; # with a right-associative `!`, this is `$a ! ($b ! $c)` -$a ! $b ! $c; # with a non-associative `!`, this is illegal -$a ! $b ! $c; # with a chain-associative `!`, this is `($a ! $b) and ($b ! $c)` -$a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` - -### Unary operators: -!$a! # with left-associative `!`, this is `(!$a)!` -!$a! # with right-associative `!`, this is `!($a!)` -!$a! # with non-associative `!`, this is illegal +## The associativity and precedence list are explained below. ## Alright, you're set to go ! @@ -180,24 +167,48 @@ $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an ar ### Their string equivalent are also avaiable : `lt`, `le`, `gt`, `ge`. 3 > 4; -## * Sort comparison -### They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). -1 <=> 4; # sort comparison for numerics -'a' leg 'b'; # sort comparison for string -$obj eqv $obj2; # sort comparison using eqv semantics - -## * Generic ordering -3 before 4; # True -'b' after 'a'; # True - ## * Range constructors 3 .. 7; # 3 to 7, both included ### `^` on either side them exclusive on that side : 3 ^..^ 7; # 3 to 7, not included (basically `4 .. 6`) # * And, Or +3 && 4; # True. Calls `.Bool` on `3` +0 || False; # False. Calls `.Bool` on `0` ## Short-circuit (and tight) $a && $b && $c; # returns the first argument that evaluates to False, or the last argument $a || $b; + +# More operators thingies ! + +## Everybody loves operators ! Let's get more of them + +## The precedence list can be found here : http://perlcabal.org/syn/S03.html#Operator_precedence +## But first, we need a little explanation about associativity : + +### Binary operators: +$a ! $b ! $c; # with a left-associative `!`, this is `($a ! $b) ! $c` +$a ! $b ! $c; # with a right-associative `!`, this is `$a ! ($b ! $c)` +$a ! $b ! $c; # with a non-associative `!`, this is illegal +$a ! $b ! $c; # with a chain-associative `!`, this is `($a ! $b) and ($b ! $c)` +$a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` + +### Unary operators: +!$a! # with left-associative `!`, this is `(!$a)!` +!$a! # with right-associative `!`, this is `!($a!)` +!$a! # with non-associative `!`, this is illegal + +## And to end the list of operators ... + +## * Sort comparison +### They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). +1 <=> 4; # sort comparison for numerics +'a' leg 'b'; # sort comparison for string +$obj eqv $obj2; # sort comparison using eqv semantics + +## * Generic ordering +3 before 4; # True +'b' after 'a'; # True + ``` -- cgit v1.2.3 From d6fa11cb750bf791d7f53fe95ed14a50e2cac60a Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sun, 13 Jul 2014 20:42:29 +0200 Subject: Some class love --- perl6.html.markdown | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index f86c3d64..fb052590 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -38,7 +38,7 @@ my $weird'variable-name_ = 5; my @array = 1, 2, 3; my @array = 'a', 'b', 'c'; # equivalent to : -my @array = ; # similar to perl5's qw, or Ruby's %w +my @array = ; # array of string, delimited by space. similar to perl5's qw, or Ruby's %w say @array[2]; # Arrays are 0-indexed @@ -57,6 +57,12 @@ say %hash; # if it's a string, you can actually use <> ## - Subs (subroutines, or functions in most other languages). Stored in variable, they use `&` sub say-hello { say "Hello, world" } +sub say-hello-to(Str $name) { # you can provide the type of an argument + # and it'll be checked at compile-time + + say "Hello, $name !"; +} + # since you can omit parenthesis to call a function with no arguments, you need to use `&` also to capture `say-hello` my &s = &say-hello; my &other-s = sub { say "anonymous function !" } @@ -64,6 +70,25 @@ my &other-s = sub { say "anonymous function !" } # `->`, lambda with arguments, and string interpolation my &lambda = -> $argument { "The argument passed to this lambda is $argument" } +### Containers +# In Perl 6, values are actually stored in "containers". +# the assignment operator asks the container on the left to store the value on its right +# When passed around, containers are marked as immutable. Which means that, in a function, +# you'll get an error if you try to mutate one of your argument. +# If you really need to, you can ask for a mutable container using `is rw` : +sub mutate($n is rw) { + $n++; + say "\$n is now $n !"; +} + +# If what you want is a copy instead, use `is copy`. + +# A sub itself returns a container, which means it can be marked as rw : +my $x = 42; +sub mod() is rw { $x } +mod() = 52; # in this case, the parentheses are mandatory +say $x; #=> 52 + ### Control Flow Structures # You don't need to put parenthesis around the condition, but that also means you always have to use brackets (`{ }`) for their body : @@ -180,6 +205,37 @@ $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an ar $a && $b && $c; # returns the first argument that evaluates to False, or the last argument $a || $b; +# Perl 6 has a quite comprehensive class system +## You declare a class with the keyword `class`, fields with `has`, methods with `method` +## `$.` declares a public field, `$!` declares a private field +## (a public field also has `$!`, which is its private interface) + +class A { + has $.field; + has Int $!private-field = 10; + + method get-value { + $.field + $!private-field + $n; + } + + method set-value($n) { + # $.field = $n; # This fails, because a public field is actually an immutable container + # (even from inside the class) + # You either need to use `is rw` on the `has` + # (which will make it mutable, even from outside the class) + # or you need to use the `$!` version : + + $!field = $n; # This works, because `$!` is always mutable + } +}; + +# Create a new instance of A with $.field set to 5 : +# note : you can't set private-field from here (more later on) +my $a = A.new(field => 5); +$a.get-value; #=> 18 +#$a.field = 5; # This fails, because the `has $.field` is lacking the `is rw` + + # More operators thingies ! ## Everybody loves operators ! Let's get more of them -- cgit v1.2.3 From f97d9c5aba4b50911c0580cccbd3950be5192c01 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sun, 13 Jul 2014 22:28:41 +0200 Subject: Moar classes. Roles. And stuff. And dispatch todo : muti dispatch --- perl6.html.markdown | 171 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 143 insertions(+), 28 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index fb052590..057c74f8 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -70,6 +70,52 @@ my &other-s = sub { say "anonymous function !" } # `->`, lambda with arguments, and string interpolation my &lambda = -> $argument { "The argument passed to this lambda is $argument" } +# add 3 to each value of an array using map : +map({ $_ + 3 }, @array); # $_ is the implicit argument (the same as for `given` and `for`) + +# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`) : +# a block doesn't have a function context (though it can have arguments), which means that if you +# return from it, you're going to return from the parent function, compare: +sub is-in(@array, $elem) { + map({ return True if $_ == $elem }, @array); # this will `return` out of `is-in` +} +sub truthy-array(@array) { + # this will produce an array of `True` and `False` : + # (you can also say `anon sub` for "anonymous subroutine") + map(sub { if $_ { return True } else { return False } }, @array); +} + +# `-> {}` and `{}` are pretty much the same thing, except taht the former can take arguments, +# and that the latter can be mistaken as a hash by the compiler + +# You can also use the "whatever star" to create an anonymous function : +map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` +map(*+*+3, @array); # also works. Same as `-> $a, $b -> { $a + $b + 3 }` + +# but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), +# you can also use the implicit argument syntax, `$^` : +map({ $^a + $^b + 3 }, @array); # same as the above + +# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` + +## Multiple Dispatch +# Perl 6 can decide which variant of a `sub` to call based on the type of the arguments, +# or on arbitrary preconditions, using `where` : + +# with types +multi sub sayit(Int $n) { # note the `multi` keyword here + say "Number: $n"; +} +multi sayit(Str $s) } # the `sub` is implicit + say "String: $s"; +} +sayit("foo"); # prints "String: foo" +sayit(True); # fails at *compile time* with "calling 'sayit' will never work with arguments of types ..." + +# with arbitrary precondition : +multi is-big(Int $n where * > 10) { True } +multi is-big(Int $) { False } + ### Containers # In Perl 6, values are actually stored in "containers". # the assignment operator asks the container on the left to store the value on its right @@ -95,6 +141,7 @@ say $x; #=> 52 ## Conditionals +# - `if` if True { say "It's true !"; } @@ -103,10 +150,12 @@ unless False { say "It's not false !"; } - # if (true) say; # Won't work -# `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching : +# - Ternary conditional +my $a = $condition ?? $value-if-true !! $value-if-false; # `??` and `!!` are like `?` and `:` in other languages' + +# - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching : given "foo bar" { # given just puts its argument into `$_`, and `when` uses it using the "smart matching" operator. when /foo/ { # you'll read about the smart-matching operator below say "Yay !"; @@ -118,7 +167,7 @@ given "foo bar" { # given just puts its argument into `$_`, and `when` uses it u ## Looping constructs -### - `loop` is an infinite loop if you don't pass it arguments, but can also be a c-style `for` : +# - `loop` is an infinite loop if you don't pass it arguments, but can also be a c-style `for` : loop { say "This is an infinite loop !"; last; # last breaks out of the loop, like the `break` keyword in other languages @@ -130,7 +179,7 @@ loop (my $i = 0; $i < 5; $i++) { say "This is a C-style for loop !"; } -### - `for` - Foreaches an array +# - `for` - Foreaches an array for @array -> $variable { say "I've found $variable !"; } @@ -147,69 +196,74 @@ if long-computation() -> $result { -# Operators +### Operators ## Since Perl languages are very much operator-based languages ## Perl 6 operators are actually just funny-looking subroutines, in syntactic categories, ## like infix:<+> (addition) or prefix: (bool not) ## The categories are : -### - "prefix" : before (like `!` in `!True`). -### - "postfix" : after (like `++` in `$a++`). -### - "infix" : in between (like `*` in `4 * 3`). -### - "circumfix" : around (like `[`-`]` in `[1, 2]`). -### - "post-circumfix" : around, after another term (like `{`-`}` in `%hash{'key'}`) +# - "prefix" : before (like `!` in `!True`). +# - "postfix" : after (like `++` in `$a++`). +# - "infix" : in between (like `*` in `4 * 3`). +# - "circumfix" : around (like `[`-`]` in `[1, 2]`). +# - "post-circumfix" : around, after another term (like `{`-`}` in `%hash{'key'}`) ## The associativity and precedence list are explained below. -## Alright, you're set to go ! +# Alright, you're set to go ! ## * Equality Checking -### - `==` is numeric comparison +# - `==` is numeric comparison 3 == 4; # False 3 != 4; # True -### - `eq` is string comparison +# - `eq` is string comparison 'a' eq 'b'; 'a' ne 'b'; # not equal 'a' !eq 'b'; # same as above -### - `eqv` is canonical equivalence +# - `eqv` is canonical equivalence (1, 2) eqv (1, 3); -### - `~~` is smart matching -### for a complete combinations list, use this table : http://perlcabal.org/syn/S03.html#Smart_matching +# - `~~` is smart matching +# for a complete combinations list, use this table : http://perlcabal.org/syn/S03.html#Smart_matching 'a' ~~ /a/; # true if matches regexp 'key' ~~ %hash; # true if key exists in hash $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an argument, returns True 1 ~~ Int; # "is of type" -### - `===` is value identity and uses `.WHICH` on the objects to compare them -### - `=:=` is container identity and uses `VAR()` on the objects to compare them +# - `===` is value identity and uses `.WHICH` on the objects to compare them +# - `=:=` is container identity and uses `VAR()` on the objects to compare them -### You also, of course, have `<`, `<=`, `>`, `>=`. -### Their string equivalent are also avaiable : `lt`, `le`, `gt`, `ge`. +# You also, of course, have `<`, `<=`, `>`, `>=`. +# Their string equivalent are also avaiable : `lt`, `le`, `gt`, `ge`. 3 > 4; ## * Range constructors 3 .. 7; # 3 to 7, both included -### `^` on either side them exclusive on that side : +# `^` on either side them exclusive on that side : 3 ^..^ 7; # 3 to 7, not included (basically `4 .. 6`) -# * And, Or +## * And, Or 3 && 4; # True. Calls `.Bool` on `3` 0 || False; # False. Calls `.Bool` on `0` -## Short-circuit (and tight) +## Short-circuit (and tight) versions of the above $a && $b && $c; # returns the first argument that evaluates to False, or the last argument $a || $b; -# Perl 6 has a quite comprehensive class system +### Object Model + +## Perl 6 has a quite comprehensive object model ## You declare a class with the keyword `class`, fields with `has`, methods with `method` ## `$.` declares a public field, `$!` declares a private field ## (a public field also has `$!`, which is its private interface) +# (Perl 6's object model ("P6Model") is very flexible, and allows you to dynamically add methods, +# change semantics, etc -- This will not be covered here, and you should refer to the Synopsis) + class A { has $.field; has Int $!private-field = 10; @@ -227,6 +281,10 @@ class A { $!field = $n; # This works, because `$!` is always mutable } + + method !private-method { + say "This method is private to the class !"; + } }; # Create a new instance of A with $.field set to 5 : @@ -235,22 +293,79 @@ my $a = A.new(field => 5); $a.get-value; #=> 18 #$a.field = 5; # This fails, because the `has $.field` is lacking the `is rw` +## Perl 6 also has inheritance (along with multiple inheritance ... Considered a misfeature by many) + +class A { + has $.val; + + submethod not-inherited { + say "This method won't be available on B."; + say "This is most useful for BUILD, which we'll see later"; + } + + method bar { $.val * 5 } +} +class B is A { # inheritance uses `is` + method foo { + say $.val; + } + + method bar { $.val * 10 } # this shadows A's `bar` +} + +my B $b .= new(val => 5); # When you use `my T $var`, `$var` starts off with `T` itself in it, so you can call `new` on it + # (`.=` is just the compound operator composed of the dot-call and of the assignment operator) + # + # Also note that `BUILD` (the method called inside `new`) will set parent properties too, + # so you can pass `val => 5` +# $b.not-inherited; # This won't work, for reasons explained above +$b.foo; # prints 5 +$b.bar; #=> 50, since it calls B's `bar` + +## Roles are supported too (also called Mixins in other languages) +role PrintableVal { + has $!counter = 0; + method print { + say $.val; + } +} + +# you "use" a mixin with "does" : +class Item does PrintableVal { + has $.val; + + # When `does`-ed, a `role` literally "mixes in" the class : + # the methods and fields are put together, which means a class can access + # the private fields/methods of its roles (but not the inverse !) : + method access { + say $!counter++; + } + + # However, this : + # method print {} + # is an error, since the compiler wouldn't know which `print` to use : + # contrarily to inheritance, methods mixed in can't be shadowed - they're put at the same "level" + + # NOTE : You can use a role as a class (with `is ROLE`). In this case, methods will be shadowed, + # since the compiler will consider `ROLE` to be a class +} + -# More operators thingies ! +### More operators thingies ! ## Everybody loves operators ! Let's get more of them ## The precedence list can be found here : http://perlcabal.org/syn/S03.html#Operator_precedence ## But first, we need a little explanation about associativity : -### Binary operators: +# - Binary operators: $a ! $b ! $c; # with a left-associative `!`, this is `($a ! $b) ! $c` $a ! $b ! $c; # with a right-associative `!`, this is `$a ! ($b ! $c)` $a ! $b ! $c; # with a non-associative `!`, this is illegal $a ! $b ! $c; # with a chain-associative `!`, this is `($a ! $b) and ($b ! $c)` $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` -### Unary operators: +# - Unary operators: !$a! # with left-associative `!`, this is `(!$a)!` !$a! # with right-associative `!`, this is `!($a!)` !$a! # with non-associative `!`, this is illegal @@ -258,7 +373,7 @@ $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` ## And to end the list of operators ... ## * Sort comparison -### They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). +# They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). 1 <=> 4; # sort comparison for numerics 'a' leg 'b'; # sort comparison for string $obj eqv $obj2; # sort comparison using eqv semantics -- cgit v1.2.3 From 6a33cbd3aee825f4cc16c82c15963c66486d1834 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Wed, 16 Jul 2014 20:07:35 +0200 Subject: Try to fix HL --- perl6.html.markdown | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 057c74f8..7029de27 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -14,10 +14,6 @@ Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM and [the MoarVM](htt ```perl6 # Single line comment start with a pound -#`( - Multiline comments use #` and a quoting construct. (), [], {}, 「」, etc, will work. -) - ### Variables # In Perl 6, you declare a lexical variable using `my` @@ -88,9 +84,11 @@ sub truthy-array(@array) { # `-> {}` and `{}` are pretty much the same thing, except taht the former can take arguments, # and that the latter can be mistaken as a hash by the compiler -# You can also use the "whatever star" to create an anonymous function : +# You can also use the "whatever star" to create an anonymous function +# (it'll stop at the furthest operator in the current expression) map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` -map(*+*+3, @array); # also works. Same as `-> $a, $b -> { $a + $b + 3 }` +map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` +say ((*+3)/5)(5); # immediatly execute the function Whatever created -- works even in parens ! # but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), # you can also use the implicit argument syntax, `$^` : @@ -116,6 +114,9 @@ sayit(True); # fails at *compile time* with "calling 'sayit' will never work wit multi is-big(Int $n where * > 10) { True } multi is-big(Int $) { False } +# you can also name these checks, by creating "subsets" : +subset Even of Int where * %% 2; + ### Containers # In Perl 6, values are actually stored in "containers". # the assignment operator asks the container on the left to store the value on its right @@ -195,7 +196,6 @@ if long-computation() -> $result { } - ### Operators ## Since Perl languages are very much operator-based languages -- cgit v1.2.3 From 3d81df43b15debcf3643a1977a83c5051b25803b Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Wed, 16 Jul 2014 20:08:01 +0200 Subject: Switch to perl to see if it fixes comments ... --- perl6.html.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 7029de27..830b8182 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -11,9 +11,13 @@ Perl 6 is a highly capable, feature-rich programming language made for the upcom Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM and [the MoarVM](http://moarvm.com). -```perl6 +```perl # Single line comment start with a pound +#`( + Multiline comments use #` and a quoting construct. (), [], {}, 「」, etc, will work. +) + ### Variables # In Perl 6, you declare a lexical variable using `my` -- cgit v1.2.3 From 78e3a442e927f858d1a83fdc005b94ffca3ee659 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Wed, 16 Jul 2014 20:08:48 +0200 Subject: Sigh. --- perl6.html.markdown | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 830b8182..30468c56 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -29,9 +29,8 @@ Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM and [the MoarVM](htt my $str = 'String'; my $str2 = "String"; # double quotes allow for interpolation -# variable names can contain but not end with simple quotes and dashes, and can contain (and end with) underscores - -my $weird'variable-name_ = 5; +# variable names can contain but not end with simple quotes and dashes, and can contain (and end with) underscores : +# my $weird'variable-name_ = 5; # works ! ## - Arrays. They represent multiple values. They start with `@` -- cgit v1.2.3 From 22ad9896836e7fc74d50c55533b13fda9ca54a32 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Wed, 16 Jul 2014 23:12:20 +0200 Subject: Move stuff around Probably for the worst ... --- perl6.html.markdown | 156 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 99 insertions(+), 57 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 30468c56..d9dce641 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -37,9 +37,9 @@ my $str2 = "String"; # double quotes allow for interpolation my @array = 1, 2, 3; my @array = 'a', 'b', 'c'; # equivalent to : -my @array = ; # array of string, delimited by space. similar to perl5's qw, or Ruby's %w +my @array = ; # array of words, delimited by space. similar to perl5's qw, or Ruby's %w -say @array[2]; # Arrays are 0-indexed +say @array[2]; # Array indices start at 0 -- This is the third element ## - Hashes @@ -68,57 +68,8 @@ my &other-s = sub { say "anonymous function !" } # `->`, lambda with arguments, and string interpolation my &lambda = -> $argument { "The argument passed to this lambda is $argument" } - -# add 3 to each value of an array using map : -map({ $_ + 3 }, @array); # $_ is the implicit argument (the same as for `given` and `for`) - -# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`) : -# a block doesn't have a function context (though it can have arguments), which means that if you -# return from it, you're going to return from the parent function, compare: -sub is-in(@array, $elem) { - map({ return True if $_ == $elem }, @array); # this will `return` out of `is-in` -} -sub truthy-array(@array) { - # this will produce an array of `True` and `False` : - # (you can also say `anon sub` for "anonymous subroutine") - map(sub { if $_ { return True } else { return False } }, @array); -} - -# `-> {}` and `{}` are pretty much the same thing, except taht the former can take arguments, -# and that the latter can be mistaken as a hash by the compiler - -# You can also use the "whatever star" to create an anonymous function -# (it'll stop at the furthest operator in the current expression) -map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` -map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` -say ((*+3)/5)(5); # immediatly execute the function Whatever created -- works even in parens ! - -# but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), -# you can also use the implicit argument syntax, `$^` : -map({ $^a + $^b + 3 }, @array); # same as the above - -# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` - -## Multiple Dispatch -# Perl 6 can decide which variant of a `sub` to call based on the type of the arguments, -# or on arbitrary preconditions, using `where` : - -# with types -multi sub sayit(Int $n) { # note the `multi` keyword here - say "Number: $n"; -} -multi sayit(Str $s) } # the `sub` is implicit - say "String: $s"; -} -sayit("foo"); # prints "String: foo" -sayit(True); # fails at *compile time* with "calling 'sayit' will never work with arguments of types ..." - -# with arbitrary precondition : -multi is-big(Int $n where * > 10) { True } -multi is-big(Int $) { False } - -# you can also name these checks, by creating "subsets" : -subset Even of Int where * %% 2; +# We're going to see how powerful Perl 6 subs are just a little down below, after seeing the basics of operators +# and control flow structures ### Containers # In Perl 6, values are actually stored in "containers". @@ -154,7 +105,10 @@ unless False { say "It's not false !"; } -# if (true) say; # Won't work +# You can also use their postfix versions, with the keyword after: +say "Quite truthy" if True; + +# if (true) say; # This doesn't work ! # - Ternary conditional my $a = $condition ?? $value-if-true !! $value-if-false; # `??` and `!!` are like `?` and `:` in other languages' @@ -189,16 +143,15 @@ for @array -> $variable { } # default variable is $_ -for array { +for @array { say "I've got $_"; } -# Note - the "lambda" `->` syntax isn't reserved to for : +# Note - the "lambda" `->` syntax isn't reserved to `for` : if long-computation() -> $result { say "The result is $result"; } - ### Operators ## Since Perl languages are very much operator-based languages @@ -248,6 +201,17 @@ $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an ar 3 .. 7; # 3 to 7, both included # `^` on either side them exclusive on that side : 3 ^..^ 7; # 3 to 7, not included (basically `4 .. 6`) +# this also works as a shortcut for `0..^N` +^10; # 0..^10 + +# This also allows us to demonstrate that Perl 6 has lazy arrays : +my @array = 1..*; # 1 to Infinite ! +say @array[^10]; # you can pass arrays as subscripts and it'll return an array of results + # this will print "1 2 3 4 5 6 7 8 9 10" (and not run out of memory !) + +# Warning, though : if you try this example in the REPL and juste put `1..*`, +# Perl 6 will be forced to try and evaluate the whole array (to print it), +# so you'll end with an infinite loop ## * And, Or 3 && 4; # True. Calls `.Bool` on `3` @@ -257,6 +221,84 @@ $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an ar $a && $b && $c; # returns the first argument that evaluates to False, or the last argument $a || $b; +## Sequence operator +# !TODO! +1, 2, 3 ... 10; + +## More on Subs ! +# Perl 6 likes functions. So, in Perl 6, functions are very powerful: + +## Multiple Dispatch +# Perl 6 can decide which variant of a `sub` to call based on the type of the arguments, +# or on arbitrary preconditions, using `where` : + +# with types +multi sub sayit(Int $n) { # note the `multi` keyword here + say "Number: $n"; +} +multi sayit(Str $s) } # the `sub` is implicit + say "String: $s"; +} +sayit("foo"); # prints "String: foo" +sayit(True); # fails at *compile time* with "calling 'sayit' will never work with arguments of types ..." + +# with arbitrary precondition: +multi is-big(Int $n where * > 10) { True } +multi is-big(Int $) { False } + +# you can also name these checks, by creating "subsets": +subset Even of Int where * %% 2; + + +# The last expression of a sub is returned automatically (though you may use the `return` keyword, of course): +sub next-index($n) { + $n + 1; +} +my $new-n = next-index(3); # $new-n is now 4 +# This is true for everything, except for the looping constructs (due to performance reasons): +# there's no purpose in building a list if we're just going to discard all the results. +# If you still want to build one, you can use the `do` prefix: (or the `gather` prefix, which we'll see later) +sub list-of($n) { + do for ^$n { # note the use of the range-to prefix operator `^` (`0..^N`) + $_ # current loop iteration + } +} +my @list3 = list-of(3); #=> (0, 1, 2) + +# We can, for example, add 3 to each value of an array using map : +my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument (the same as for `given` and `for`) + +# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`) : +# a block doesn't have a function context (though it can have arguments), which means that if you +# return from it, you're going to return from the parent function, compare: +sub is-in(@array, $elem) { + # this will `return` out of `is-in` sub + # once the condition evaluated to True, the loop won't be run anymore + map({ return True if $_ == $elem }, @array); +} +sub truthy-array(@array) { + # this will produce an array of `True` and `False` : + # (you can also say `anon sub` for "anonymous subroutine") + map(sub { if $_ { return True } else { return False } }, @array); # returns the correct value, even in a `if` +} + +# `-> {}` and `{}` are pretty much the same thing, except that the former can take arguments, +# and that the latter can be mistaken as a hash by the compiler + +# You can also use the "whatever star" to create an anonymous function +# (it'll stop at the furthest operator in the current expression) +my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` +my @arrayplus3 = map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` +say ((*+3)/5)(5); # immediatly execute the function Whatever created -- works even in parens ! + +# but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), +# you can also use the implicit argument syntax, `$^` : +map({ $^a + $^b + 3 }, @array); # same as the above + +# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` + + + ### Object Model ## Perl 6 has a quite comprehensive object model -- cgit v1.2.3 From 1652740cb92b73ce1b441484c7a5e1ae1e835764 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sat, 19 Jul 2014 01:49:07 +0200 Subject: Well met Sequence / FF --- perl6.html.markdown | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index d9dce641..b4918115 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -204,14 +204,16 @@ $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an ar # this also works as a shortcut for `0..^N` ^10; # 0..^10 -# This also allows us to demonstrate that Perl 6 has lazy arrays : +# This also allows us to demonstrate that Perl 6 has lazy arrays, using the Whatever Star : my @array = 1..*; # 1 to Infinite ! say @array[^10]; # you can pass arrays as subscripts and it'll return an array of results # this will print "1 2 3 4 5 6 7 8 9 10" (and not run out of memory !) +# Note : when reading an infinite list, Perl 6 will "reify" the elements it needs, then keep them in memory +# They won't be calculated more than once. # Warning, though : if you try this example in the REPL and juste put `1..*`, # Perl 6 will be forced to try and evaluate the whole array (to print it), -# so you'll end with an infinite loop +# so you'll end with an infinite loop. ## * And, Or 3 && 4; # True. Calls `.Bool` on `3` @@ -222,8 +224,19 @@ $a && $b && $c; # returns the first argument that evaluates to False, or the las $a || $b; ## Sequence operator -# !TODO! -1, 2, 3 ... 10; +# The sequence operator is one of Perl 6's most powerful features : +# it's composed of first, on the left, the list you want Perl 6 to deduce from (and might include a closure), +# and on the right, a value or the predicate for when to stop, or even Whatever for a lazy infinite list +my @list = 1, 2, 3 ... 10; # basic deducing +#my @list = 1, 3, 6 ... 10; # this throws you into an infinite loop, because Perl 6 can't figure out the end +my @list = 1, 2, 3 ...^ 10; # as with ranges, you can exclude the last element (when the predicate matches) +my @list = 1, 3, 9 ... * > 30; # you can use a predicate (with the Whatever Star, here) +my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalent to the above) +my @primes = 1, 1, *+* ... *; # lazy infinite list of prime numbers, computed using a closure ! +my @primes = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above) +say @primes[^10]; #=> 1 1 2 3 5 8 13 21 34 55 +# Note : as for ranges, once reified, elements aren't re-calculated. +# That's why `@primes[^100]` will take a long time the first time you print it, then be instant ## More on Subs ! # Perl 6 likes functions. So, in Perl 6, functions are very powerful: @@ -415,7 +428,7 @@ $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` !$a! # with right-associative `!`, this is `!($a!)` !$a! # with non-associative `!`, this is illegal -## And to end the list of operators ... +## Last part of the operator list : ## * Sort comparison # They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). @@ -427,4 +440,25 @@ $obj eqv $obj2; # sort comparison using eqv semantics 3 before 4; # True 'b' after 'a'; # True +## * Flip Flop +# The flip flop operator (spelled `ff` in Perl 6 and sometimes `..` in other languages such as Perl 5 and Ruby), +# is an operator that takes two boolean values (like a predicate) and keep track of their change as internal state. +# The flip-flop will return `false` until its left side return true, then return true until its right side return true. +# You can also exclude either side (iteration when the left side became true, or the right side became true), +# using the `^` like with ranges. +# Let's start with an example : +for { + if $_ eq 'met' ^ff $_ eq 'meet' { # excludes "met" + .say + } +} +# This will print "young hero we shall meet" (excluding "met"): +# the flip-flop will start returning `True` when it first encounters "met" +# (but will still return `False` for "met" itself, due to the leading `^` on `ff`), +# until it sees "meet", which is when it'll start returning `False`. +# A flip-flop can change state as many times as needed: +for { + .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop", + # this prints "print this printing again" +} ``` -- cgit v1.2.3 From 7314a87f9d49128376cf1320ea85e7a4411e5758 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sat, 19 Jul 2014 01:54:05 +0200 Subject: ff * --- perl6.html.markdown | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index b4918115..76f08248 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -11,6 +11,9 @@ Perl 6 is a highly capable, feature-rich programming language made for the upcom Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM and [the MoarVM](http://moarvm.com). +Meta-note : the triple pound signs are here to denote headlines, double paragraphs, single notes. +`#=>` represents the output of a command. + ```perl # Single line comment start with a pound @@ -459,6 +462,19 @@ for { # A flip-flop can change state as many times as needed: for { .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop", - # this prints "print this printing again" + #=> "print this printing again" +} + +# you might also use a Whatever Star, which is equivalent to `True` for the left side or `False` for the right : +for (1, 3, 60, 3, 40, 60) { + .say if $_ > 50 ff *; # Once the flip-flop reached a number greater than 50, it'll never go back to `False` + #=> 60 3 40 60 +} + +# You can also use this property to create an `If` that'll not execute the first time : +for { + .say if * ^ff *; # the flip-flop is `True` and never goes back to `False`, + # but the `^` makes it *not run* on the first iteration + #=> b c } ``` -- cgit v1.2.3 From be796e988a522260aedc380e7f10e3e50b65138e Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sun, 20 Jul 2014 01:25:20 +0200 Subject: Phasers ! and more: Add default Add a bit on exceptions (TODO fail/warn) --- perl6.html.markdown | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 76f08248..44fc94e9 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -116,14 +116,18 @@ say "Quite truthy" if True; # - Ternary conditional my $a = $condition ?? $value-if-true !! $value-if-false; # `??` and `!!` are like `?` and `:` in other languages' -# - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching : -given "foo bar" { # given just puts its argument into `$_`, and `when` uses it using the "smart matching" operator. - when /foo/ { # you'll read about the smart-matching operator below +# - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching. +# given just puts its argument into `$_`, and `when` uses it using the "smart matching" operator. +given "foo bar" { + when /foo/ { # you'll read about the smart-matching operator below -- just know `when` uses it say "Yay !"; } when $_.chars > 50 { # smart matching anything with True gives True, so you can also put "normal" conditionals say "Quite a long string !"; } + default { # same as `when *` (using the Whatever Star) + say "Something else" + } } ## Looping constructs @@ -150,6 +154,12 @@ for @array { say "I've got $_"; } +for @array { + next if $_ == 3; # you can skip to the next iteration (like `continue` in C-like languages) + redo if $_ == 4; # you can re-do the iteration, keeping the same topic variable (`$_`) + last if $_ == 5; # you can also break out of a loop (like `break` in C-like languages) +} + # Note - the "lambda" `->` syntax isn't reserved to `for` : if long-computation() -> $result { say "The result is $result"; @@ -411,6 +421,74 @@ class Item does PrintableVal { # since the compiler will consider `ROLE` to be a class } +### Exceptions +# Exceptions are built on top of classes, usually in the package `X` (like `X::IO`). +# Unlike many other languages, in Perl 6, you put the `CATCH` block *within* the block to `try`. +# By default, a `try` has a `CATCH` block that catches any exception (`CATCH { default {} }`). +# You can redefine it using `when`s (and `default`) to handle the exceptions you want: +try { + open 'foo'; + CATCH { + when X::AdHoc { say "unable to open file !" } + # any other exception will be re-raised, since we don't have a `default` + } +} + +# You can throw an exception using `die`: +die X::AdHoc.new(payload => 'Error !'); +# TODO warn +# TODO fail +# TODO CONTROL + +### Phasers +# Phasers in Perl 6 are blocks that happen at determined points of time in your program +# When the program is compiled, when a for loop runs, when you leave a block, when +# an exception gets thrown ... (`CATCH` is actually a phaser !) +# Some of them can be used for their return values, some of them can't +# (those that can have a "[*]" in the beginning of their explanation text). +# Let's have a look ! + +## * Compile-time phasers +BEGIN { say "[*] Runs at compile time, as soon as possible, only once" } +CHECK { say "[*] Runs at compile time, instead as late as possible, only once" } + +## * Run-time phasers +INIT { say "[*] Runs at run time, as soon as possible, only once" } +END { say "Runs at run time, as late as possible, only once" } + +## * Block phasers +ENTER { say "[*] Runs everytime you enter a block, repeats on loop blocks" } +LEAVE { say "Runs everytime you leave a block, even when an exception happened. Repeats on loop blocks." } + +PRE { say "Asserts a precondition at every block entry, before ENTER (especially useful for loops)" } +POST { say "Asserts a postcondition at every block exit, after LEAVE (especially useful for loops)" } + +## * Block/exceptions phasers +sub { + KEEP { say "Runs when you exit a block successfully (without throwing an exception)" } + UNDO { say "Runs when you exit a block unsuccessfully (by throwing an exception)" } +} + +## * Loop phasers +for ^5 { + FIRST { say "[*] The first time the loop is run, before ENTER" } + NEXT { say "At loop continuation time, before LEAVE" } + LAST { say "At loop termination time, after LEAVE" } +} + +## * Role/class phasers +COMPOSE { "When a role is composed into a class. /!\ NOT YET IMPLEMENTED /!\" } + +# They allow for cute trick or clever code ...: +say "This code took " ~ (time - CHECK time) ~ "s to run"; + +# ... or clever organization: +sub do-db-stuff { + ENTER $db.start-transaction; # create a new transaction everytime we enter the sub + KEEP $db.commit; # commit the transaction if all went well + UNDO $db.rollback; # or rollback if all hell broke loose +} + ### More operators thingies ! -- cgit v1.2.3 From 37a38181a82c6cf3897ea357dab57f71d51f94e8 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Mon, 21 Jul 2014 23:18:55 +0200 Subject: Scoping explanations (lexical / dynamic) Start on packages. ff/fff like `when` ff vs fff ~~ on Bool // and ^^ operators TODO warn/fail/control --- perl6.html.markdown | 95 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 87 insertions(+), 8 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 44fc94e9..70799b29 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -113,7 +113,7 @@ say "Quite truthy" if True; # if (true) say; # This doesn't work ! -# - Ternary conditional +# - Ternary conditional, "?? !!" my $a = $condition ?? $value-if-true !! $value-if-false; # `??` and `!!` are like `?` and `:` in other languages' # - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching. @@ -202,6 +202,7 @@ if long-computation() -> $result { 'key' ~~ %hash; # true if key exists in hash $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an argument, returns True 1 ~~ Int; # "is of type" +1 ~~ True; # smart-matching against a boolean always returns that boolean (and will warn). # - `===` is value identity and uses `.WHICH` on the objects to compare them # - `=:=` is container identity and uses `VAR()` on the objects to compare them @@ -323,7 +324,38 @@ map({ $^a + $^b + 3 }, @array); # same as the above # Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` - +### Scoping +# In Perl 6, contrarily to many scripting languages (Python, Ruby, PHP, for example), +# you are to declare your variables before using them. You already saw it, with `my`. +# (there are other declarator keywords, like `our`, `has` and `state`, but we'll talk about them later) +# This is called "lexical scoping", where in inner blocks, you can access variables from outer blocks. +my $foo = 'Foo'; +sub foo { + my $bar = 'Bar'; + sub bar { + say "$foo $bar"; + } + &bar; # return the function +} +foo()(); #=> 'Foo Bar' + +# As you can see, `$foo` and `$bar` were captured. +# But if we were to try and use `$bar` outside of `foo`, the variable would be undefined. +# (and you'd get a compile time error) + +# Perl 6 has another kind of scope : dynamic scope. +# They use the twigil (composed sigil) `*` to mark dynamically-scoped variables: +my $*a = 1; +# Dyamically-scoped variables depend on the current call stack, instead of the current block stack. +sub foo { + my $*foo = 1; + bar(); # call `bar` in-place +} +sub bar { + say $*foo; # Perl 6 will look into the call stack instead, and find `foo`'s `$*a`, + # even though the blocks aren't nested (they're call-nested). + #=> 1 +} ### Object Model @@ -440,6 +472,28 @@ die X::AdHoc.new(payload => 'Error !'); # TODO fail # TODO CONTROL +### Packages +# Packages play a big part in a language, and Perl is well-known for CPAN, +# the Comprehensive Perl Archive Network. +# You can declare a mdule using the `module` keyword, and they can be nested: +module Hello::World { # bracketed form + # declarations here +} +module Parse::Text; # file-scoped form + +# You can use a module (bring its declarations into scope) with `use` +use JSON::Tiny; # if you installed Rakudo* or Panda, you'll have this module +say from-json('[1]').perl; #=> [1] + +# Any class, role, is also a module +my $actions = JSON::Tiny::Actions.new; + +# We'll see how to export variables and subs in the next part: + +### Declarators +TODO: my, our, state, constant. + + ### Phasers # Phasers in Perl 6 are blocks that happen at determined points of time in your program # When the program is compiled, when a for loop runs, when you leave a block, when @@ -521,22 +575,47 @@ $obj eqv $obj2; # sort comparison using eqv semantics 3 before 4; # True 'b' after 'a'; # True +## * Short-circuit default operator +# Like `or` and `||`, but instead returns the first *defined* value : +say Any // Nil // 0 // 5; #=> 5 + +## * Short-circuit exclusive or (XOR) +# Returns `True` if one (and only one) of its arguments is true +say True ^^ False; #=> True + ## * Flip Flop -# The flip flop operator (spelled `ff` in Perl 6 and sometimes `..` in other languages such as Perl 5 and Ruby), -# is an operator that takes two boolean values (like a predicate) and keep track of their change as internal state. -# The flip-flop will return `false` until its left side return true, then return true until its right side return true. -# You can also exclude either side (iteration when the left side became true, or the right side became true), -# using the `^` like with ranges. +# The flip flop operators (`ff` and `fff`, equivalent to Perl 5/Ruby's `..` and `...`). +# are operators that take two predicates to test: +# They are `False` until their left side returns `True`, then are `True` until their right side returns `True`. +# Like for ranges, you can exclude the iteration when it became `True`/`False` by using `^` on either side. # Let's start with an example : for { - if $_ eq 'met' ^ff $_ eq 'meet' { # excludes "met" + # by default, `ff`/`fff` smart-match (`~~`) against `$_`: + if 'met' ^ff 'meet' { # won't enter the if for "met" (explained in details below). .say } + + if rand == 0 ff rand == 1 { # compare variables other than `$_` + say "This ... probably will never run ..."; + } } # This will print "young hero we shall meet" (excluding "met"): # the flip-flop will start returning `True` when it first encounters "met" # (but will still return `False` for "met" itself, due to the leading `^` on `ff`), # until it sees "meet", which is when it'll start returning `False`. + +# The difference between `ff` (flip-flop) and `fff` (flip-flop) is that +# `ff` will test its right side just as its left side changes to `True`, +# and can get back to `False` right away (*except* it'll be `True` for the iteration that matched) +# while `fff` will wait for the next iteration to try its right side, once its left side changed: +.say if 'B' ff 'B' for ; #=> B B + # because the right-hand-side was tested directly (and returned `True`). + # "B"s are still printed since it matched that time + # (it just went back to `False` right away) +.say if 'B' fff 'B' for ; #=> B C B + # because the right-hand-side wasn't tested until `$_` became "C" + # (and thus did not match directly). + # A flip-flop can change state as many times as needed: for { .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop", -- cgit v1.2.3 From 1d931a37f2d428bc2d783f1a8597455db70a23de Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Tue, 22 Jul 2014 22:55:27 +0200 Subject: Declarators mention the six model clearer description of package/module etc clearer description of `$.` vs `$!` --- perl6.html.markdown | 97 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 17 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 70799b29..5ead6560 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -360,29 +360,27 @@ sub bar { ### Object Model ## Perl 6 has a quite comprehensive object model -## You declare a class with the keyword `class`, fields with `has`, methods with `method` -## `$.` declares a public field, `$!` declares a private field -## (a public field also has `$!`, which is its private interface) +## You declare a class with the keyword `class`, fields with `has`, methods with `method`. +## In Perl 6, every field is private, and named `$!attr`, but if you declare it with `$.`, +## you get a public (immutable) accessor along with it. # (Perl 6's object model ("P6Model") is very flexible, and allows you to dynamically add methods, # change semantics, etc -- This will not be covered here, and you should refer to the Synopsis) class A { - has $.field; + has $.field; # `$.field` is immutable. Use `$!field` from inside the class to modify it. + has $.other-field is rw; # You can, however, mark a public field as being read/write. has Int $!private-field = 10; - + method get-value { $.field + $!private-field + $n; } method set-value($n) { - # $.field = $n; # This fails, because a public field is actually an immutable container - # (even from inside the class) - # You either need to use `is rw` on the `has` - # (which will make it mutable, even from outside the class) - # or you need to use the `$!` version : - - $!field = $n; # This works, because `$!` is always mutable + # $.field = $n; # As stated before, you can't use the `$.` immutable version. + $!field = $n; # This works, because `$!` is always mutable. + + $.other-field = 5; # This works, because `$.other-field` was declared `rw` (mutable). } method !private-method { @@ -394,7 +392,8 @@ class A { # note : you can't set private-field from here (more later on) my $a = A.new(field => 5); $a.get-value; #=> 18 -#$a.field = 5; # This fails, because the `has $.field` is lacking the `is rw` +#$a.field = 5; # This fails, because the `has $.field` is immutable +$a.other-field = 10; # This, however, works, because the public field is mutable (`rw`). ## Perl 6 also has inheritance (along with multiple inheritance ... Considered a misfeature by many) @@ -473,25 +472,89 @@ die X::AdHoc.new(payload => 'Error !'); # TODO CONTROL ### Packages -# Packages play a big part in a language, and Perl is well-known for CPAN, +# Packages are a way to reuse code. Packages are like "namespaces", and any element of the six model +# (`module`, `role`, `class`, `grammar`, `subset` and `enum`) are actually packages. +# (you can say that packages are the lowest common denomitor between them) +# Packages play a big part in a language, as Perl is well-known for CPAN, # the Comprehensive Perl Archive Network. -# You can declare a mdule using the `module` keyword, and they can be nested: +# You usually don't use packages directly : you use `class Package::Name::Here;`, or if you +# only want to export variables/subs, you can use `module`: module Hello::World { # bracketed form + # if `Hello` doesn't exist yet, it'll just be created as an "empty package stub" + # that can be redeclared as something else later. # declarations here } module Parse::Text; # file-scoped form +grammar Parse::Text::Grammar { # A grammar is a fine package, which you could `use` +} + +# NOTE for Perl 5 users: even though the `package` keyword exists, +# the braceless form is invalid (to catch a "perl5ism"). This will error out: +# package Foo; # because Perl 6 will think the entire file is Perl 5 +# Just use `module` or the brace version of `package`. # You can use a module (bring its declarations into scope) with `use` use JSON::Tiny; # if you installed Rakudo* or Panda, you'll have this module say from-json('[1]').perl; #=> [1] -# Any class, role, is also a module +# As said before, any part of the six model is also a package. +# Since `JSON::Tiny` uses (its own) `JSON::Tiny::Actions` class, you can use it: my $actions = JSON::Tiny::Actions.new; # We'll see how to export variables and subs in the next part: ### Declarators -TODO: my, our, state, constant. +# In Perl 6, you get different behaviors based on how you declare a variable. +# You've already seen `my` and `has`, we'll now explore the others. + +## * `our` (happens at `INIT` time -- see "Phasers" below) +# Along with `my`, there are several others declarators you can use. +# The first one you'll want for the previous part is `our`. +# (All packagish things (`class`, `role`, etc) are `our` by default) +# it's like `my`, but it also creates a package variable: +module Foo::Bar { + our $n = 1; # note: you can't put a type constraint on an `our` variable + our sub inc { + our sub available { # if you try to make scoped `sub`s `our` ... Better know what you're doing (Don't !). + say "Don't do that. Seriously. You'd get burned."; + } + my sub unavailable { # `my sub` is the default + say "Can't access me from outside, I'm my !"; + } + } + + say ++$n; # lexically-scoped variables are still available +} +say $Foo::Bar::n; #=> 1 +Foo::Bar::inc; #=> 2 +Foo::Bar::inc; #=> 3 + +## * `constant` (happens at `BEGIN` time) +# You can use the `constant` keyword to declare a compile-time variable/symbol: +constant Pi = 3.14; +constant $var = 1; + +## * `state` (happens at run time, but only once) +# State variables are only executed one time +# (they exist in other langages such as C as `static`) +sub fixed-rand { + state $val = rand; + say $rand; +} +fixed-rand for ^10; # will print the same number 10 times + +# Note, however, that they exist separately in different enclosing contexts. +# If you declare a function with a `state` within a loop, it'll re-create the variable +# for each iteration of loop. See: +for ^5 -> $a { + sub foo { + state $val = rand; # This will be a different value for every value of `$a` + } + for ^5 -> $b { + say foo; # This will print the same value 5 times, but only 5. Next iteration will re-run `rand` + } +} + ### Phasers -- cgit v1.2.3 From 38c167867203d6192060a853ceae4c47eee581ad Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Thu, 24 Jul 2014 23:05:44 +0200 Subject: More on sub arguments. Declare your own subs !! More on hash: add colonpair syntax Flatten argument list with `|` Multi on :$! TODO: `MAIN` TODO: meta ops --- perl6.html.markdown | 232 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 222 insertions(+), 10 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 5ead6560..d7864b7f 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -35,6 +35,10 @@ my $str2 = "String"; # double quotes allow for interpolation # variable names can contain but not end with simple quotes and dashes, and can contain (and end with) underscores : # my $weird'variable-name_ = 5; # works ! +my $bool = True; # `True` and `False` are Perl 6's boolean +my $inverse = !$bool; # You can invert a bool with the prefix `!` operator +my $forced-bool = so $str; # And you can use the prefix `so` operator which turns its operand into a Bool + ## - Arrays. They represent multiple values. They start with `@` my @array = 1, 2, 3; @@ -44,14 +48,26 @@ my @array = ; # array of words, delimited by space. similar to perl5's qw say @array[2]; # Array indices start at 0 -- This is the third element -## - Hashes +say "Interpolate an array using [] : @array[]"; #=> Interpolate an array using [] : a b c + +## - Hashes. Key-Value Pairs. +# Hashes are actually arrays of Pairs (`Key => Value`), "flattened" to remove duplicated keys. my %hash = 1 => 2, 3 => 4; -my %hash = autoquoted => "key", +my %hash = autoquoted => "key", # keys are auto-quoted "some other" => "value", # trailing commas are okay ; -my %hash = # you can also create a hash from an even-numbered array +my %hash = ; # you can also create a hash from an even-numbered array +my %hash = key1 => 'value1', key2 => 'value2'; # same as this + +# You can also use the "colon pair" syntax: (especially handy for named parameters that you'll see later) +my %hash = :w(1), # equivalent to `w => 1` + # this is useful for the `True` shortcut: + :truey, # equivalent to `:truey(True)`, or `truey => True` + # and for the `False` one: + :!falsey, # equivalent to `:falsey(False)`, or `falsey => False` + ; say %hash{'key1'}; # You can use {} to get the value from a key say %hash; # if it's a string, you can actually use <> @@ -69,6 +85,123 @@ sub say-hello-to(Str $name) { # you can provide the type of an argument my &s = &say-hello; my &other-s = sub { say "anonymous function !" } +# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" +sub as-many($head, *@rest) { # the `*@` slurpy will basically "take everything else". + # Note: you can have parameters *before* (like here) a slurpy one, but not *after*. + say @rest.join(' / ') ~ " !"; +} +say as-many('Happy', 'Happy', 'Birthday'); #=> Happy Birthday ! + # Note that the splat did not consume the parameter before. + +## You can call a function with an array using the "argument list flattening" operator `|` +# (it's not actually the only feature of the operator, but it's one of them) +sub concat3($a, $b, $c) { + say "$a, $b, $c"; +} +concat3(|@array); #=> a, b, c + # `@array` got "flattened" as a part of the argument list + +## It can also have optional arguments: +sub with-optional($arg?) { # the "?" marks the argument optional + say "I might return `(Any)` if I don't have an argument passed, or I'll return my argument"; + $arg; +} +with-optional; # returns Any +with-optional(); # returns Any +with-optional(1); # returns 1 + +## You can also give them a default value when they're not passed: +sub hello-to($name = "World") { + say "Hello, $name !"; +} +hello-to; #=> Hello, World ! +hello-to(); #=> Hello, World ! +hello-to('You'); #=> Hello, You ! + +## You can also, by using a syntax akin to the one of hashes (yay unification !), +## pass *named* arguments to a `sub`. +sub with-named($normal-arg, :$named) { + say $normal-arg + $named; +} +with-named(1, named => 6); #=> 7 +with-named(2, :named(5)); #=> 7 +with-named(3, :4named); #=> 7 + # (special colon pair syntax for numbers) + +with-named(3); # warns, because we tried to use the undefined $named + # in a `+`: by default, named arguments are *optional* + +# To make a named argument mandatory, you can use `?`'s inverse, `!` +sub with-mandatory-named(:$str!) { + say "$named !"; +} +with-mandatory-named(str => "My String"); #=> My String +with-mandatory-named; # run time error: "Required named parameter not passed" +with-mandatory-named(3); # run time error: "Too many positional parameters passed" + +## If a sub takes a named boolean argument ... +sub takes-a-bool($name, :$bool) { + say "$name takes $bool"; +} +# ... you can use the same "short boolean" hash syntax: +takes-a-bool('config', :bool); # config takes True +takes-a-bool('config', :!bool); # config takes False +# or you can use the "adverb" form: +takes-a-bool('config'):bool; #=> config takes True +takes-a-bool('config'):!bool; #=> config takes False +# You'll learn to love (or maybe hate, eh) that syntax later. + + +## You can also provide your named arguments with defaults: +sub named-def(:$def = 5) { + say $def; +} +named-def; #=> 5 +named-def(:10def); #=> 10 +named-def(def => 15); #=> 15 + +## There's more to come, but we're going to end this paragraph with a really powerful feature: +## Unpacking ! It's the ability to "extract" arrays and keys. It'll work in `my`s and parameters. +my ($a, $b) = 1, 2; +say $a; #=> 1 +my ($, $, $c) = 1, 2, 3; # keep the non-interesting anonymous +say $c; #=> 3 + +my ($head, *@tail) = 1, 2, 3; # Yes, it's the same as with "slurpy subs" +my (*@small) = 1; + +sub foo(@array [$fst, $snd]) { + say "My first is $fst, my second is $snd ! All in all, I'm @array[]."; # (remember the `[]` to interpolate the array) +} +foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 1 2 + + +# If you're not using the array itself, you can also keep it anonymous, much like a scalar: +sub first-of-array(@ [$fst]) { $fst } +first-of-array(@small); #=> 1 +first-of-array(@tail); # errors with "Too many positional parameters passed" (the array is too big) + +# You can also use a slurp ... +sub slurp-in-array(@ [$fst, *@rest]) { # you could decide to keep `*@rest` anonymous + say $fst + @rest.elems; +} +slurp-in-array(@tail); #=> 3 + +# You could even extract on a slurpy (but it's pretty useless ;-).) +sub fst(*@ [$fst]) { # or simply : `sub fst($fst) { ... }` + say $fst; +} +fst(1); #=> 1 +fst(1, 2); # errors with "Too many positional parameters passed" + +# Lou can also destructure hashes (and classes, which you'll learn about later !) +sub key-of(% (:value($val), :qua($qua))) { + say "Got val $val, $qua times."; +} +# Then call it with a hash: (you need to keep the brackets for it to be a hash) +key-of({value => 1}); +#key-of(%hash); # the same (for an equivalent `%hash`) + # `->`, lambda with arguments, and string interpolation my &lambda = -> $argument { "The argument passed to this lambda is $argument" } # We're going to see how powerful Perl 6 subs are just a little down below, after seeing the basics of operators @@ -225,7 +358,7 @@ say @array[^10]; # you can pass arrays as subscripts and it'll return an array o # Note : when reading an infinite list, Perl 6 will "reify" the elements it needs, then keep them in memory # They won't be calculated more than once. -# Warning, though : if you try this example in the REPL and juste put `1..*`, +# Warning, though: if you try this example in the REPL and juste put `1..*`, # Perl 6 will be forced to try and evaluate the whole array (to print it), # so you'll end with an infinite loop. @@ -252,12 +385,12 @@ say @primes[^10]; #=> 1 1 2 3 5 8 13 21 34 55 # Note : as for ranges, once reified, elements aren't re-calculated. # That's why `@primes[^100]` will take a long time the first time you print it, then be instant -## More on Subs ! -# Perl 6 likes functions. So, in Perl 6, functions are very powerful: +### More on Subs ! +# Perl 6 likes functions. So, in Perl 6, they are very powerful: ## Multiple Dispatch # Perl 6 can decide which variant of a `sub` to call based on the type of the arguments, -# or on arbitrary preconditions, using `where` : +# or on arbitrary preconditions, like with a type or a `where`: # with types multi sub sayit(Int $n) { # note the `multi` keyword here @@ -270,13 +403,32 @@ sayit("foo"); # prints "String: foo" sayit(True); # fails at *compile time* with "calling 'sayit' will never work with arguments of types ..." # with arbitrary precondition: -multi is-big(Int $n where * > 10) { True } -multi is-big(Int $) { False } +multi is-big(Int $n where * > 50) { "Yes !" } # using a closure +multi is-big(Int $ where 10..50) { "Quite." } # this uses smart-matching (could use a regexp, etc) +multi is-big(Int $) { "No" } # you can also name these checks, by creating "subsets": subset Even of Int where * %% 2; +multi odd-or-even(Even) { "Even" } # the main case using the type. We don't name the argument +multi odd-or-even($) { "Odd" } # "else" + +# You can even dispatch based on a positional's argument presence ! +multi with-or-without-you(:$with!) { # make it mandatory to be able to dispatch against it + say "I can live ! Actually, I can't."; +} +multi with-or-without-you { + say "Definitely can't live."; +} +# This is very, very useful for many purposes, like `MAIN` subs (covered later), +# and even the language itself is using it in several places. +# `is`, for example, is actually a `multi sub` named `trait_mod:`, and it works off that. +# `is rw`, for example, is a dispatch to a function with this signature: +# sub trait_mod:(Routine $r, :$rw!) {} +# (commented because running this would probably lead to some surprising side-effects !) + +# ---- # The last expression of a sub is returned automatically (though you may use the `return` keyword, of course): sub next-index($n) { $n + 1; @@ -626,7 +778,67 @@ $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` !$a! # with right-associative `!`, this is `!($a!)` !$a! # with non-associative `!`, this is illegal -## Last part of the operator list : +## Create your own operators ! +# Okay, you've been reading all of that, so I guess I should try to show you something exciting. +# I'll tell you a little secret (actually not): In Perl 6, all operators are actually just funny-looking subroutines. +# You can declare an operator just like you declare a sub: +sub prefix:($winner) { # refer to the operator categories + # (yes, it's the "words operator" `<>`) + say "$winner Won !"; +} +win "The King"; #=> The King Won ! + # (prefix is before) + +# you can still call the sub with its "full name" +say prefix:(True); #=> False + +sub postfix:(Int $n) { + [*] 2..$n; # using the reduce meta-operator ... See below ;-) ! +} +say 5!; #=> 120 + # (postfix is after) + + +sub infix:(Int $n, Block $r) { # infix in the middle + for ^$n { + $r(); # needs the parentheses because it's a scalar + } +} +3 times -> { say "hello" }; #=> hello + #=> hello + #=> hello + +# For circumfix and post-circumfix ones +sub circumfix:<[ ]>(Int $n) { + $n ** $n +} +say [5]; #=> 3125 + # circumfix is around + +sub postcircumfix:<{ }>(Str $s, Int $idx) { # post-circumfix is "after a term, around something" + $s.substr($idx, 1); +} +say "abc"{1}; #=> b + # after the term `"abc"`, and around the index (1) + +# This really means a lot -- because everything in Perl 6 uses this. +# For example, to delete a key from a hash, you use the `:delete` adverb (named argument) +%h{$key}:delete; +# equivalent to: +postcircumfix:<{ }>(%h, $key, :delete); +# It's *all* using the same building blocks! Syntactic categories (prefix infix ...), +# named arguments (adverbs), ..., used to build the language are available to you. + +# (you are, obviously, recommended against making an operator out of *everything* -- +# with great power comes great responsibility) + +## Meta operators ! +# Oh boy, get ready. Get ready, because we're dwelving deep into the rabbit's hole, and you probably won't want +# to go back to other languages after reading that (I'm sure you don't want to already at that point). + +# - Reduce meta-operator + +## End of the operator list: ## * Sort comparison # They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). -- cgit v1.2.3 From 4c6e3e772334b29bfc14985e84b05136db0e52e1 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Thu, 24 Jul 2014 23:06:44 +0200 Subject: Fix compose's inside ... --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index d7864b7f..ad68ccb3 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -746,7 +746,7 @@ for ^5 { } ## * Role/class phasers -COMPOSE { "When a role is composed into a class. /!\ NOT YET IMPLEMENTED /!\" } +COMPOSE { "When a role is composed into a class. /!\ NOT YET IMPLEMENTED" } # They allow for cute trick or clever code ...: say "This code took " ~ (time - CHECK time) ~ "s to run"; -- cgit v1.2.3 From 82d65fb3b20e17c90a35fc9cea05d33b961a963c Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Fri, 25 Jul 2014 11:30:50 +0200 Subject: Move stuff around. ventica++ --- perl6.html.markdown | 100 +++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 48 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index ad68ccb3..0e6b55fc 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -226,6 +226,58 @@ sub mod() is rw { $x } mod() = 52; # in this case, the parentheses are mandatory say $x; #=> 52 +# The last expression of a sub is returned automatically (though you may use the `return` keyword, of course): +sub next-index($n) { + $n + 1; +} +my $new-n = next-index(3); # $new-n is now 4 +# This is true for everything, except for the looping constructs (due to performance reasons): +# there's no purpose in building a list if we're just going to discard all the results. +# If you still want to build one, you can use the `do` prefix: (or the `gather` prefix, which we'll see later) +sub list-of($n) { + do for ^$n { # note the use of the range-to prefix operator `^` (`0..^N`) + $_ # current loop iteration + } +} +my @list3 = list-of(3); #=> (0, 1, 2) + +# We can, for example, add 3 to each value of an array using map : +my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument (the same as for `given` and `for`) + +# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`) : +# a block doesn't have a function context (though it can have arguments), which means that if you +# return from it, you're going to return from the parent function, compare: +sub is-in(@array, $elem) { + # this will `return` out of `is-in` sub + # once the condition evaluated to True, the loop won't be run anymore + map({ return True if $_ == $elem }, @array); +} +sub truthy-array(@array) { + # this will produce an array of `True` and `False` : + # (you can also say `anon sub` for "anonymous subroutine") + map(sub { if $_ { return True } else { return False } }, @array); # returns the correct value, even in a `if` +} + +# `-> {}` and `{}` are pretty much the same thing, except that the former can take arguments, +# and that the latter can be mistaken as a hash by the compiler + +# You can also use the "whatever star" to create an anonymous function +# (it'll stop at the furthest operator in the current expression) +my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` +my @arrayplus3 = map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` +say (*/2)(4); #=> 2 + # Immediatly execute the function Whatever created. +say ((*+3)/5)(5); #=> 1.6 + # works even in parens ! + +# but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), +# you can also use the implicit argument syntax, `$^` : +map({ $^a + $^b + 3 }, @array); # same as the above + +# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` + + + ### Control Flow Structures # You don't need to put parenthesis around the condition, but that also means you always have to use brackets (`{ }`) for their body : @@ -428,54 +480,6 @@ multi with-or-without-you { # (commented because running this would probably lead to some surprising side-effects !) -# ---- -# The last expression of a sub is returned automatically (though you may use the `return` keyword, of course): -sub next-index($n) { - $n + 1; -} -my $new-n = next-index(3); # $new-n is now 4 -# This is true for everything, except for the looping constructs (due to performance reasons): -# there's no purpose in building a list if we're just going to discard all the results. -# If you still want to build one, you can use the `do` prefix: (or the `gather` prefix, which we'll see later) -sub list-of($n) { - do for ^$n { # note the use of the range-to prefix operator `^` (`0..^N`) - $_ # current loop iteration - } -} -my @list3 = list-of(3); #=> (0, 1, 2) - -# We can, for example, add 3 to each value of an array using map : -my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument (the same as for `given` and `for`) - -# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`) : -# a block doesn't have a function context (though it can have arguments), which means that if you -# return from it, you're going to return from the parent function, compare: -sub is-in(@array, $elem) { - # this will `return` out of `is-in` sub - # once the condition evaluated to True, the loop won't be run anymore - map({ return True if $_ == $elem }, @array); -} -sub truthy-array(@array) { - # this will produce an array of `True` and `False` : - # (you can also say `anon sub` for "anonymous subroutine") - map(sub { if $_ { return True } else { return False } }, @array); # returns the correct value, even in a `if` -} - -# `-> {}` and `{}` are pretty much the same thing, except that the former can take arguments, -# and that the latter can be mistaken as a hash by the compiler - -# You can also use the "whatever star" to create an anonymous function -# (it'll stop at the furthest operator in the current expression) -my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` -my @arrayplus3 = map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` -say ((*+3)/5)(5); # immediatly execute the function Whatever created -- works even in parens ! - -# but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), -# you can also use the implicit argument syntax, `$^` : -map({ $^a + $^b + 3 }, @array); # same as the above - -# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` - ### Scoping # In Perl 6, contrarily to many scripting languages (Python, Ruby, PHP, for example), # you are to declare your variables before using them. You already saw it, with `my`. -- cgit v1.2.3 From a85a05a139c8c2e23686987b0cc5deffe599283d Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Fri, 25 Jul 2014 11:47:56 +0200 Subject: Move stuff around, again. Need to teach it in the right order ... --- perl6.html.markdown | 247 ++++++++++++++++++++++++++-------------------------- 1 file changed, 123 insertions(+), 124 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 0e6b55fc..599f207c 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -126,7 +126,7 @@ sub with-named($normal-arg, :$named) { with-named(1, named => 6); #=> 7 with-named(2, :named(5)); #=> 7 with-named(3, :4named); #=> 7 - # (special colon pair syntax for numbers) + # (special colon pair syntax for numbers, mainly useful for `:2nd` etc) with-named(3); # warns, because we tried to use the undefined $named # in a `+`: by default, named arguments are *optional* @@ -160,53 +160,6 @@ named-def; #=> 5 named-def(:10def); #=> 10 named-def(def => 15); #=> 15 -## There's more to come, but we're going to end this paragraph with a really powerful feature: -## Unpacking ! It's the ability to "extract" arrays and keys. It'll work in `my`s and parameters. -my ($a, $b) = 1, 2; -say $a; #=> 1 -my ($, $, $c) = 1, 2, 3; # keep the non-interesting anonymous -say $c; #=> 3 - -my ($head, *@tail) = 1, 2, 3; # Yes, it's the same as with "slurpy subs" -my (*@small) = 1; - -sub foo(@array [$fst, $snd]) { - say "My first is $fst, my second is $snd ! All in all, I'm @array[]."; # (remember the `[]` to interpolate the array) -} -foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 1 2 - - -# If you're not using the array itself, you can also keep it anonymous, much like a scalar: -sub first-of-array(@ [$fst]) { $fst } -first-of-array(@small); #=> 1 -first-of-array(@tail); # errors with "Too many positional parameters passed" (the array is too big) - -# You can also use a slurp ... -sub slurp-in-array(@ [$fst, *@rest]) { # you could decide to keep `*@rest` anonymous - say $fst + @rest.elems; -} -slurp-in-array(@tail); #=> 3 - -# You could even extract on a slurpy (but it's pretty useless ;-).) -sub fst(*@ [$fst]) { # or simply : `sub fst($fst) { ... }` - say $fst; -} -fst(1); #=> 1 -fst(1, 2); # errors with "Too many positional parameters passed" - -# Lou can also destructure hashes (and classes, which you'll learn about later !) -sub key-of(% (:value($val), :qua($qua))) { - say "Got val $val, $qua times."; -} -# Then call it with a hash: (you need to keep the brackets for it to be a hash) -key-of({value => 1}); -#key-of(%hash); # the same (for an equivalent `%hash`) - -# `->`, lambda with arguments, and string interpolation -my &lambda = -> $argument { "The argument passed to this lambda is $argument" } -# We're going to see how powerful Perl 6 subs are just a little down below, after seeing the basics of operators -# and control flow structures - ### Containers # In Perl 6, values are actually stored in "containers". # the assignment operator asks the container on the left to store the value on its right @@ -226,57 +179,6 @@ sub mod() is rw { $x } mod() = 52; # in this case, the parentheses are mandatory say $x; #=> 52 -# The last expression of a sub is returned automatically (though you may use the `return` keyword, of course): -sub next-index($n) { - $n + 1; -} -my $new-n = next-index(3); # $new-n is now 4 -# This is true for everything, except for the looping constructs (due to performance reasons): -# there's no purpose in building a list if we're just going to discard all the results. -# If you still want to build one, you can use the `do` prefix: (or the `gather` prefix, which we'll see later) -sub list-of($n) { - do for ^$n { # note the use of the range-to prefix operator `^` (`0..^N`) - $_ # current loop iteration - } -} -my @list3 = list-of(3); #=> (0, 1, 2) - -# We can, for example, add 3 to each value of an array using map : -my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument (the same as for `given` and `for`) - -# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`) : -# a block doesn't have a function context (though it can have arguments), which means that if you -# return from it, you're going to return from the parent function, compare: -sub is-in(@array, $elem) { - # this will `return` out of `is-in` sub - # once the condition evaluated to True, the loop won't be run anymore - map({ return True if $_ == $elem }, @array); -} -sub truthy-array(@array) { - # this will produce an array of `True` and `False` : - # (you can also say `anon sub` for "anonymous subroutine") - map(sub { if $_ { return True } else { return False } }, @array); # returns the correct value, even in a `if` -} - -# `-> {}` and `{}` are pretty much the same thing, except that the former can take arguments, -# and that the latter can be mistaken as a hash by the compiler - -# You can also use the "whatever star" to create an anonymous function -# (it'll stop at the furthest operator in the current expression) -my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` -my @arrayplus3 = map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` -say (*/2)(4); #=> 2 - # Immediatly execute the function Whatever created. -say ((*+3)/5)(5); #=> 1.6 - # works even in parens ! - -# but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), -# you can also use the implicit argument syntax, `$^` : -map({ $^a + $^b + 3 }, @array); # same as the above - -# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` - - ### Control Flow Structures @@ -298,11 +200,11 @@ say "Quite truthy" if True; # if (true) say; # This doesn't work ! -# - Ternary conditional, "?? !!" -my $a = $condition ?? $value-if-true !! $value-if-false; # `??` and `!!` are like `?` and `:` in other languages' +# - Ternary conditional, "?? !!" (like `x ? y : z` in some other languages) +my $a = $condition ?? $value-if-true !! $value-if-false; # - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching. -# given just puts its argument into `$_`, and `when` uses it using the "smart matching" operator. +# given just puts its argument into `$_` (like a block), and `when` uses it using the "smart matching" operator. given "foo bar" { when /foo/ { # you'll read about the smart-matching operator below -- just know `when` uses it say "Yay !"; @@ -329,12 +231,12 @@ loop (my $i = 0; $i < 5; $i++) { say "This is a C-style for loop !"; } -# - `for` - Foreaches an array +# - `for` - Passes through an array for @array -> $variable { say "I've found $variable !"; } -# default variable is $_ +# default variable is $_ (like a block) for @array { say "I've got $_"; } @@ -378,7 +280,7 @@ if long-computation() -> $result { 'a' ne 'b'; # not equal 'a' !eq 'b'; # same as above -# - `eqv` is canonical equivalence +# - `eqv` is canonical equivalence (or "deep equality") (1, 2) eqv (1, 3); # - `~~` is smart matching @@ -401,7 +303,7 @@ $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an ar # `^` on either side them exclusive on that side : 3 ^..^ 7; # 3 to 7, not included (basically `4 .. 6`) # this also works as a shortcut for `0..^N` -^10; # 0..^10 +^10; # means 0..^10 # This also allows us to demonstrate that Perl 6 has lazy arrays, using the Whatever Star : my @array = 1..*; # 1 to Infinite ! @@ -422,23 +324,102 @@ say @array[^10]; # you can pass arrays as subscripts and it'll return an array o $a && $b && $c; # returns the first argument that evaluates to False, or the last argument $a || $b; -## Sequence operator -# The sequence operator is one of Perl 6's most powerful features : -# it's composed of first, on the left, the list you want Perl 6 to deduce from (and might include a closure), -# and on the right, a value or the predicate for when to stop, or even Whatever for a lazy infinite list -my @list = 1, 2, 3 ... 10; # basic deducing -#my @list = 1, 3, 6 ... 10; # this throws you into an infinite loop, because Perl 6 can't figure out the end -my @list = 1, 2, 3 ...^ 10; # as with ranges, you can exclude the last element (when the predicate matches) -my @list = 1, 3, 9 ... * > 30; # you can use a predicate (with the Whatever Star, here) -my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalent to the above) -my @primes = 1, 1, *+* ... *; # lazy infinite list of prime numbers, computed using a closure ! -my @primes = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above) -say @primes[^10]; #=> 1 1 2 3 5 8 13 21 34 55 -# Note : as for ranges, once reified, elements aren't re-calculated. -# That's why `@primes[^100]` will take a long time the first time you print it, then be instant +### More on subs ! -### More on Subs ! -# Perl 6 likes functions. So, in Perl 6, they are very powerful: +## There's more to come, but we're going to end this paragraph with a really powerful feature: +## Unpacking ! It's the ability to "extract" arrays and keys. It'll work in `my`s and parameters. +my ($a, $b) = 1, 2; +say $a; #=> 1 +my ($, $, $c) = 1, 2, 3; # keep the non-interesting anonymous +say $c; #=> 3 + +my ($head, *@tail) = 1, 2, 3; # Yes, it's the same as with "slurpy subs" +my (*@small) = 1; + +sub foo(@array [$fst, $snd]) { + say "My first is $fst, my second is $snd ! All in all, I'm @array[]."; # (remember the `[]` to interpolate the array) +} +foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 1 2 + + +# If you're not using the array itself, you can also keep it anonymous, much like a scalar: +sub first-of-array(@ [$fst]) { $fst } +first-of-array(@small); #=> 1 +first-of-array(@tail); # errors with "Too many positional parameters passed" (the array is too big) + +# You can also use a slurp ... +sub slurp-in-array(@ [$fst, *@rest]) { # you could decide to keep `*@rest` anonymous + say $fst + @rest.elems; +} +slurp-in-array(@tail); #=> 3 + +# You could even extract on a slurpy (but it's pretty useless ;-).) +sub fst(*@ [$fst]) { # or simply : `sub fst($fst) { ... }` + say $fst; +} +fst(1); #=> 1 +fst(1, 2); # errors with "Too many positional parameters passed" + +# You can also destructure hashes (and classes, which you'll learn about later !) +sub key-of(% (:value($val), :qua($qua))) { + say "Got val $val, $qua times."; +} + +# Then call it with a hash: (you need to keep the brackets for it to be a hash) +key-of({value => 1}); +#key-of(%hash); # the same (for an equivalent `%hash`) + +## The last expression of a sub is returned automatically (though you may use the `return` keyword, of course): +sub next-index($n) { + $n + 1; +} +my $new-n = next-index(3); # $new-n is now 4 +# This is true for everything, except for the looping constructs (due to performance reasons): +# there's no purpose in building a list if we're just going to discard all the results. +# If you still want to build one, you can use the `do` prefix: (or the `gather` prefix, which we'll see later) +sub list-of($n) { + do for ^$n { # note the use of the range-to prefix operator `^` (`0..^N`) + $_ # current loop iteration + } +} +my @list3 = list-of(3); #=> (0, 1, 2) + +## You can create a lambda with `-> {}` ("pointy block") or `{}` ("block") +my &lambda = -> $argument { "The argument passed to this lambda is $argument" } +# `-> {}` and `{}` are pretty much the same thing, except that the former can take arguments, +# and that the latter can be mistaken as a hash by the parser. + +# We can, for example, add 3 to each value of an array using map: +my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument + +# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`) : +# a block doesn't have a function context (though it can have arguments), which means that if you +# return from it, you're going to return from the parent function, compare: +sub is-in(@array, $elem) { + # this will `return` out of `is-in` sub + # once the condition evaluated to True, the loop won't be run anymore + map({ return True if $_ == $elem }, @array); +} +sub truthy-array(@array) { + # this will produce an array of `True` and `False` : + # (you can also say `anon sub` for "anonymous subroutine") + map(sub { if $_ { return True } else { return False } }, @array); # returns the correct value, even in a `if` +} + +# You can also use the "whatever star" to create an anonymous function +# (it'll stop at the furthest operator in the current expression) +my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` +my @arrayplus3 = map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` +say (*/2)(4); #=> 2 + # Immediatly execute the function Whatever created. +say ((*+3)/5)(5); #=> 1.6 + # works even in parens ! + +# but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), +# you can also use the implicit argument syntax, `$^` : +map({ $^a + $^b + 3 }, @array); # same as the above + +# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` ## Multiple Dispatch # Perl 6 can decide which variant of a `sub` to call based on the type of the arguments, @@ -448,7 +429,7 @@ say @primes[^10]; #=> 1 1 2 3 5 8 13 21 34 55 multi sub sayit(Int $n) { # note the `multi` keyword here say "Number: $n"; } -multi sayit(Str $s) } # the `sub` is implicit +multi sayit(Str $s) } # the `sub` is the default say "String: $s"; } sayit("foo"); # prints "String: foo" @@ -477,7 +458,7 @@ multi with-or-without-you { # `is`, for example, is actually a `multi sub` named `trait_mod:`, and it works off that. # `is rw`, for example, is a dispatch to a function with this signature: # sub trait_mod:(Routine $r, :$rw!) {} -# (commented because running this would probably lead to some surprising side-effects !) +# (commented because running this would probably lead to some very surprising side-effects !) ### Scoping @@ -844,6 +825,24 @@ postcircumfix:<{ }>(%h, $key, :delete); ## End of the operator list: + +## Sequence operator +# The sequence operator is one of Perl 6's most powerful features: +# it's composed of first, on the left, the list you want Perl 6 to deduce from (and might include a closure), +# and on the right, a value or the predicate for when to stop, or even Whatever for a lazy infinite list. +my @list = 1, 2, 3 ... 10; # basic deducing +#my @list = 1, 3, 6 ... 10; # this throws you into an infinite loop, because Perl 6 can't figure out the end +my @list = 1, 2, 3 ...^ 10; # as with ranges, you can exclude the last element (when the predicate matches) +my @list = 1, 3, 9 ... * > 30; # you can use a predicate (with the Whatever Star, here) +my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalent to the above) +my @fib = 1, 1, *+* ... *; # lazy infinite list of prime numbers, computed using a closure ! +my @fib = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above) +say @fib[^10]; #=> 1 1 2 3 5 8 13 21 34 55 + # (using a range as the index) +# Note : as for ranges, once reified, elements aren't re-calculated. +# That's why `@primes[^100]` will take a long time the first time you print it, then be instant + + ## * Sort comparison # They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). 1 <=> 4; # sort comparison for numerics -- cgit v1.2.3 From 8631ca09f48b7fdbc3becbb232b67e53c1206ae1 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Fri, 25 Jul 2014 23:23:13 +0200 Subject: More fixes thanks to @timo++'s feedback. --- perl6.html.markdown | 82 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 30 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 599f207c..6cacc672 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -24,7 +24,7 @@ Meta-note : the triple pound signs are here to denote headlines, double paragrap ### Variables # In Perl 6, you declare a lexical variable using `my` - +a # Perl 6 has 4 variable types : ## - Scalars. They represent a single value. They start with a `$` @@ -39,7 +39,7 @@ my $bool = True; # `True` and `False` are Perl 6's boolean my $inverse = !$bool; # You can invert a bool with the prefix `!` operator my $forced-bool = so $str; # And you can use the prefix `so` operator which turns its operand into a Bool -## - Arrays. They represent multiple values. They start with `@` +## - Arrays. They represent multiple values. Their name start with `@`. my @array = 1, 2, 3; my @array = 'a', 'b', 'c'; @@ -51,11 +51,11 @@ say @array[2]; # Array indices start at 0 -- This is the third element say "Interpolate an array using [] : @array[]"; #=> Interpolate an array using [] : a b c ## - Hashes. Key-Value Pairs. -# Hashes are actually arrays of Pairs (`Key => Value`), "flattened" to remove duplicated keys. - +# Hashes are actually arrays of Pairs (`Key => Value`), +# except they get "flattened", removing duplicated keys. my %hash = 1 => 2, 3 => 4; -my %hash = autoquoted => "key", # keys are auto-quoted +my %hash = autoquoted => "key", # keys *can* get auto-quoted "some other" => "value", # trailing commas are okay ; my %hash = ; # you can also create a hash from an even-numbered array @@ -81,13 +81,15 @@ sub say-hello-to(Str $name) { # you can provide the type of an argument say "Hello, $name !"; } -# since you can omit parenthesis to call a function with no arguments, you need to use `&` also to capture `say-hello` +# since you can omit parenthesis to call a function with no arguments, +# you need "&" in the name to capture `say-hello` my &s = &say-hello; my &other-s = sub { say "anonymous function !" } # A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" sub as-many($head, *@rest) { # the `*@` slurpy will basically "take everything else". - # Note: you can have parameters *before* (like here) a slurpy one, but not *after*. + # Note: you can have parameters *before* (like here) a slurpy one, + # but not *after*. say @rest.join(' / ') ~ " !"; } say as-many('Happy', 'Happy', 'Birthday'); #=> Happy Birthday ! @@ -124,18 +126,22 @@ sub with-named($normal-arg, :$named) { say $normal-arg + $named; } with-named(1, named => 6); #=> 7 +# There's one gotcha to be aware of, here: +# If you quote your key, Perl 6 won't be able to see it as compile time, +# and you'll have a single Pair object as a positional paramater. + with-named(2, :named(5)); #=> 7 with-named(3, :4named); #=> 7 # (special colon pair syntax for numbers, mainly useful for `:2nd` etc) -with-named(3); # warns, because we tried to use the undefined $named - # in a `+`: by default, named arguments are *optional* +with-named(3); # warns, because we tried to use the undefined $named in a `+`: + # by default, named arguments are *optional* # To make a named argument mandatory, you can use `?`'s inverse, `!` sub with-mandatory-named(:$str!) { say "$named !"; } -with-mandatory-named(str => "My String"); #=> My String +with-mandatory-named(str => "My String"); #=> My String ! with-mandatory-named; # run time error: "Required named parameter not passed" with-mandatory-named(3); # run time error: "Too many positional parameters passed" @@ -160,6 +166,9 @@ named-def; #=> 5 named-def(:10def); #=> 10 named-def(def => 15); #=> 15 +# -- Note: we're going to learn *more* on subs really soon, +# but we need to grasp a few more things to understand their real power. Ready? + ### Containers # In Perl 6, values are actually stored in "containers". # the assignment operator asks the container on the left to store the value on its right @@ -176,13 +185,14 @@ sub mutate($n is rw) { # A sub itself returns a container, which means it can be marked as rw : my $x = 42; sub mod() is rw { $x } -mod() = 52; # in this case, the parentheses are mandatory +mod() = 52; # in this case, the parentheses are mandatory (else Perl 6 thinks it's a "term") say $x; #=> 52 ### Control Flow Structures -# You don't need to put parenthesis around the condition, but that also means you always have to use brackets (`{ }`) for their body : +# You don't need to put parenthesis around the condition, +# but that also means you always have to use brackets (`{ }`) for their body : ## Conditionals @@ -204,12 +214,14 @@ say "Quite truthy" if True; my $a = $condition ?? $value-if-true !! $value-if-false; # - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching. -# given just puts its argument into `$_` (like a block), and `when` uses it using the "smart matching" operator. +# given just puts its argument into `$_` (like a block), +# and `when` uses it using the "smart matching" operator. given "foo bar" { when /foo/ { # you'll read about the smart-matching operator below -- just know `when` uses it say "Yay !"; } - when $_.chars > 50 { # smart matching anything with True gives True, so you can also put "normal" conditionals + when $_.chars > 50 { # smart matching anything with True (`$a ~~ True`) is True, + # so you can also put "normal" conditionals. say "Quite a long string !"; } default { # same as `when *` (using the Whatever Star) @@ -325,8 +337,9 @@ $a && $b && $c; # returns the first argument that evaluates to False, or the las $a || $b; ### More on subs ! +# As we said before, Perl 6 has *really* powerful subs. +# We're going to see a few more key concepts that make them better than in any other language :-). -## There's more to come, but we're going to end this paragraph with a really powerful feature: ## Unpacking ! It's the ability to "extract" arrays and keys. It'll work in `my`s and parameters. my ($a, $b) = 1, 2; say $a; #=> 1 @@ -337,7 +350,8 @@ my ($head, *@tail) = 1, 2, 3; # Yes, it's the same as with "slurpy subs" my (*@small) = 1; sub foo(@array [$fst, $snd]) { - say "My first is $fst, my second is $snd ! All in all, I'm @array[]."; # (remember the `[]` to interpolate the array) + say "My first is $fst, my second is $snd ! All in all, I'm @array[]."; + # (^ remember the `[]` to interpolate the array) } foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 1 2 @@ -361,6 +375,8 @@ fst(1); #=> 1 fst(1, 2); # errors with "Too many positional parameters passed" # You can also destructure hashes (and classes, which you'll learn about later !) +# The syntax is basically `%hash-name (:key($variable-to-store-value-in))`. +# The hash can stay anonymous if you only need the values you extracted. sub key-of(% (:value($val), :qua($qua))) { say "Got val $val, $qua times."; } @@ -392,18 +408,19 @@ my &lambda = -> $argument { "The argument passed to this lambda is $argument" } # We can, for example, add 3 to each value of an array using map: my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument -# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`) : -# a block doesn't have a function context (though it can have arguments), which means that if you +# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`): +# a block doesn't have a "function context" (though it can have arguments), which means that if you # return from it, you're going to return from the parent function, compare: sub is-in(@array, $elem) { - # this will `return` out of `is-in` sub + # this will `return` out of the `is-in` sub # once the condition evaluated to True, the loop won't be run anymore map({ return True if $_ == $elem }, @array); } sub truthy-array(@array) { - # this will produce an array of `True` and `False` : + # this will produce an array of `True` and `False`: # (you can also say `anon sub` for "anonymous subroutine") - map(sub { if $_ { return True } else { return False } }, @array); # returns the correct value, even in a `if` + map(sub { if $_ { return True } else { return False } }, @array); + # ^ the `return` only returns from the anonymous `sub` } # You can also use the "whatever star" to create an anonymous function @@ -419,7 +436,7 @@ say ((*+3)/5)(5); #=> 1.6 # you can also use the implicit argument syntax, `$^` : map({ $^a + $^b + 3 }, @array); # same as the above -# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $ b / $a }` +# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $b / $a }` ## Multiple Dispatch # Perl 6 can decide which variant of a `sub` to call based on the type of the arguments, @@ -501,7 +518,7 @@ sub bar { ## In Perl 6, every field is private, and named `$!attr`, but if you declare it with `$.`, ## you get a public (immutable) accessor along with it. -# (Perl 6's object model ("P6Model") is very flexible, and allows you to dynamically add methods, +# (Perl 6's object model ("SixModel") is very flexible, and allows you to dynamically add methods, # change semantics, etc -- This will not be covered here, and you should refer to the Synopsis) class A { @@ -552,9 +569,10 @@ class B is A { # inheritance uses `is` method bar { $.val * 10 } # this shadows A's `bar` } -my B $b .= new(val => 5); # When you use `my T $var`, `$var` starts off with `T` itself in it, so you can call `new` on it - # (`.=` is just the compound operator composed of the dot-call and of the assignment operator) - # +my B $b .= new(val => 5); # When you use `my T $var`, `$var` starts off with `T` itself in it, + # so you can call `new` on it. + # (`.=` is just the compound operator composed of the dot-call and of the assignment operator + # `$a .= b` is the same as `$a = $a.b`) # Also note that `BUILD` (the method called inside `new`) will set parent properties too, # so you can pass `val => 5` # $b.not-inherited; # This won't work, for reasons explained above @@ -765,7 +783,9 @@ $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` ## Create your own operators ! # Okay, you've been reading all of that, so I guess I should try to show you something exciting. -# I'll tell you a little secret (actually not): In Perl 6, all operators are actually just funny-looking subroutines. +# I'll tell you a little secret (actually not): +# In Perl 6, all operators are actually just funny-looking subroutines. + # You can declare an operator just like you declare a sub: sub prefix:($winner) { # refer to the operator categories # (yes, it's the "words operator" `<>`) @@ -818,8 +838,9 @@ postcircumfix:<{ }>(%h, $key, :delete); # with great power comes great responsibility) ## Meta operators ! -# Oh boy, get ready. Get ready, because we're dwelving deep into the rabbit's hole, and you probably won't want -# to go back to other languages after reading that (I'm sure you don't want to already at that point). +# Oh boy, get ready. Get ready, because we're dwelving deep into the rabbit's hole, +# and you probably won't want to go back to other languages after reading that. +# (I'm guessing you don't want to already at that point). # - Reduce meta-operator @@ -900,7 +921,8 @@ for "print this printing again" } -# you might also use a Whatever Star, which is equivalent to `True` for the left side or `False` for the right : +# you might also use a Whatever Star, +# which is equivalent to `True` for the left side or `False` for the right: for (1, 3, 60, 3, 40, 60) { .say if $_ > 50 ff *; # Once the flip-flop reached a number greater than 50, it'll never go back to `False` #=> 60 3 40 60 -- cgit v1.2.3 From 61351ee266141d57ee83fe5be08f480cf2de3969 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sat, 26 Jul 2014 00:37:36 +0200 Subject: Fix explanations about $_ @masak++ --- perl6.html.markdown | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 6cacc672..f804ef86 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -213,11 +213,18 @@ say "Quite truthy" if True; # - Ternary conditional, "?? !!" (like `x ? y : z` in some other languages) my $a = $condition ?? $value-if-true !! $value-if-false; -# - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching. -# given just puts its argument into `$_` (like a block), +# - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching, +# and thanks to Perl 6's "topic variable", $_. +# This variable contains the default argument of a block, +# a loop's current iteration (unless explicitly named), etc. +# Given simply puts its argument into `$_` (like a block would do), # and `when` uses it using the "smart matching" operator. +# Since other Perl 6 constructs use this variable (as said before, like `for`, blocks, etc), +# this means the powerful `when` is not only applicable along with a `given`, +# but instead anywhere a `$_` exists. given "foo bar" { when /foo/ { # you'll read about the smart-matching operator below -- just know `when` uses it + # this is equivalent to `if $_ ~~ /foo/` say "Yay !"; } when $_.chars > 50 { # smart matching anything with True (`$a ~~ True`) is True, @@ -248,9 +255,14 @@ for @array -> $variable { say "I've found $variable !"; } -# default variable is $_ (like a block) +# As we saw with given, for's default "current iteration" variable is `$_`. +# That means you can use `when` in a `for` just like you were in a when. for @array { say "I've got $_"; + + .say; # This is also allowed. + # A dot call with no "topic" (receiver) is sent to `$_` by default + $_.say; # the above and this are equivalent. } for @array { -- cgit v1.2.3 From 8b168d41cca11286de283fdebf2549afdb186fc0 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sat, 26 Jul 2014 01:11:31 +0200 Subject: More clarifications. @masak++ --- perl6.html.markdown | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index f804ef86..6c13b793 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -197,6 +197,10 @@ say $x; #=> 52 ## Conditionals # - `if` +# Before talking about `if`, we need to know which values are "Truthy" (represent True), +# and which are "Falsey" (or "Falsy") -- meaning they represent False. +# Only these values are Falsey: (), 0, "0", Nil, A type, and of course False itself. +# Every other value is Truthy. if True { say "It's true !"; } @@ -341,7 +345,7 @@ say @array[^10]; # you can pass arrays as subscripts and it'll return an array o # so you'll end with an infinite loop. ## * And, Or -3 && 4; # True. Calls `.Bool` on `3` +3 && 4; # 4, which is Truthy. Calls `.Bool` on `4` and gets `True`. 0 || False; # False. Calls `.Bool` on `0` ## Short-circuit (and tight) versions of the above @@ -448,7 +452,7 @@ say ((*+3)/5)(5); #=> 1.6 # you can also use the implicit argument syntax, `$^` : map({ $^a + $^b + 3 }, @array); # same as the above -# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, b { $b / $a }` +# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, $b { $b / $a }` ## Multiple Dispatch # Perl 6 can decide which variant of a `sub` to call based on the type of the arguments, @@ -813,24 +817,28 @@ sub postfix:(Int $n) { [*] 2..$n; # using the reduce meta-operator ... See below ;-) ! } say 5!; #=> 120 - # (postfix is after) + # Postfix operators (after) have to come *directly* after the term. + # No whitespace. You can use parentheses to disambiguate, i.e. `(5!)!` sub infix:(Int $n, Block $r) { # infix in the middle for ^$n { - $r(); # needs the parentheses because it's a scalar + $r(); # You need the explicit parentheses to call the function in `$r`, + # else you'd be referring at the variable itself, kind of like with `&r`. } } 3 times -> { say "hello" }; #=> hello #=> hello #=> hello + # You're very recommended to put spaces + # around your infix operator calls. # For circumfix and post-circumfix ones sub circumfix:<[ ]>(Int $n) { $n ** $n } say [5]; #=> 3125 - # circumfix is around + # circumfix is around. Again, not whitespace. sub postcircumfix:<{ }>(Str $s, Int $idx) { # post-circumfix is "after a term, around something" $s.substr($idx, 1); @@ -915,7 +923,7 @@ for { # (but will still return `False` for "met" itself, due to the leading `^` on `ff`), # until it sees "meet", which is when it'll start returning `False`. -# The difference between `ff` (flip-flop) and `fff` (flip-flop) is that +# The difference between `ff` (awk-style) and `fff` (sed-style) is that # `ff` will test its right side just as its left side changes to `True`, # and can get back to `False` right away (*except* it'll be `True` for the iteration that matched) # while `fff` will wait for the next iteration to try its right side, once its left side changed: @@ -935,8 +943,8 @@ for 50 ff *; # Once the flip-flop reached a number greater than 50, it'll never go back to `False` +for (1, 3, 60, 3, 40, 60) { # Note: the parenthesis are superfluous here -- sometimes called "superstitious" + .say if $_ > 50 ff *; # Once the flip-flop reaches a number greater than 50, it'll never go back to `False` #=> 60 3 40 60 } -- cgit v1.2.3 From 5ce43af7b741a9ea1e9db8dbd5eee5d0ff3b4c2a Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sun, 27 Jul 2014 23:11:46 +0200 Subject: Meta operator: reduce and zip (and combined !) Little note about compound operators --- perl6.html.markdown | 80 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 15 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 6c13b793..b21cb1fa 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -27,7 +27,7 @@ Meta-note : the triple pound signs are here to denote headlines, double paragrap a # Perl 6 has 4 variable types : -## - Scalars. They represent a single value. They start with a `$` +## * Scalars. They represent a single value. They start with a `$` my $str = 'String'; my $str2 = "String"; # double quotes allow for interpolation @@ -39,7 +39,7 @@ my $bool = True; # `True` and `False` are Perl 6's boolean my $inverse = !$bool; # You can invert a bool with the prefix `!` operator my $forced-bool = so $str; # And you can use the prefix `so` operator which turns its operand into a Bool -## - Arrays. They represent multiple values. Their name start with `@`. +## * Arrays. They represent multiple values. Their name start with `@`. my @array = 1, 2, 3; my @array = 'a', 'b', 'c'; @@ -50,7 +50,7 @@ say @array[2]; # Array indices start at 0 -- This is the third element say "Interpolate an array using [] : @array[]"; #=> Interpolate an array using [] : a b c -## - Hashes. Key-Value Pairs. +## * Hashes. Key-Value Pairs. # Hashes are actually arrays of Pairs (`Key => Value`), # except they get "flattened", removing duplicated keys. my %hash = 1 => 2, @@ -72,7 +72,7 @@ my %hash = :w(1), # equivalent to `w => 1` say %hash{'key1'}; # You can use {} to get the value from a key say %hash; # if it's a string, you can actually use <> -## - Subs (subroutines, or functions in most other languages). Stored in variable, they use `&` +## * Subs (subroutines, or functions in most other languages). Stored in variable, they use `&` sub say-hello { say "Hello, world" } sub say-hello-to(Str $name) { # you can provide the type of an argument @@ -348,10 +348,15 @@ say @array[^10]; # you can pass arrays as subscripts and it'll return an array o 3 && 4; # 4, which is Truthy. Calls `.Bool` on `4` and gets `True`. 0 || False; # False. Calls `.Bool` on `0` -## Short-circuit (and tight) versions of the above +## * Short-circuit (and tight) versions of the above $a && $b && $c; # returns the first argument that evaluates to False, or the last argument $a || $b; +# And because you're going to want them, you also have composed assignment operators: +$a *= 2; # multiply and assignment +$b %%= 5; # divisible by and assignment +$c .= say; # method call and assignment + ### More on subs ! # As we said before, Perl 6 has *really* powerful subs. # We're going to see a few more key concepts that make them better than in any other language :-). @@ -619,7 +624,7 @@ class Item does PrintableVal { # is an error, since the compiler wouldn't know which `print` to use : # contrarily to inheritance, methods mixed in can't be shadowed - they're put at the same "level" - # NOTE : You can use a role as a class (with `is ROLE`). In this case, methods will be shadowed, + # NOTE: You can use a role as a class (with `is ROLE`). In this case, methods will be shadowed, # since the compiler will consider `ROLE` to be a class } @@ -785,14 +790,14 @@ sub do-db-stuff { ## The precedence list can be found here : http://perlcabal.org/syn/S03.html#Operator_precedence ## But first, we need a little explanation about associativity : -# - Binary operators: +# * Binary operators: $a ! $b ! $c; # with a left-associative `!`, this is `($a ! $b) ! $c` $a ! $b ! $c; # with a right-associative `!`, this is `$a ! ($b ! $c)` $a ! $b ! $c; # with a non-associative `!`, this is illegal $a ! $b ! $c; # with a chain-associative `!`, this is `($a ! $b) and ($b ! $c)` $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` -# - Unary operators: +# * Unary operators: !$a! # with left-associative `!`, this is `(!$a)!` !$a! # with right-associative `!`, this is `!($a!)` !$a! # with non-associative `!`, this is illegal @@ -861,13 +866,58 @@ postcircumfix:<{ }>(%h, $key, :delete); # Oh boy, get ready. Get ready, because we're dwelving deep into the rabbit's hole, # and you probably won't want to go back to other languages after reading that. # (I'm guessing you don't want to already at that point). - -# - Reduce meta-operator - -## End of the operator list: - - -## Sequence operator +# Meta-operators, as their name suggests, are *composed* operators. +# Basically, they're operators that apply another operator. + +## * Reduce meta-operator +# It's a prefix meta-operator that takes a binary functions and one or many lists. +# If it doesn't get passed any argument, it either return a "default value" for this operator +# (a value that'd be non-meaningful if contained in a list) or `Any` if there's none. +# Otherwise, it pops an element from the list(s) one at a time, and applies the binary function +# to the last result (or the list's first element) and the popped element. +# To sum a list, you could use the reduce meta-operator with `+`, i.e.: +say [+] 1, 2, 3; #=> 6 +# equivalent to `(1+2)+3` +say [*] 1..5; #=> 120 +# equivalent to `((((1*2)*3)*4)*5)`. + +# You can reduce with any operator, not just with mathematical ones. +# For example, you could reduce with `//` to get the first defined element of a list: +say [//] Nil, Any, False, 1, 5; #=> False + # (Falsey, but still defined) + + +# Default value examples: +say [*] (); #=> 1 +say [+] (); #=> 0 + # In both cases, they're results that, if they were contained in the lists, + # wouldn't have any impact on the final value (since N*1=N and N+0=N). +say [//]; #=> (Any) + # There's no "default value" for `//` + +# You can also call it with a function you made up, using double brackets: +sub add($a, $b) { $a + $b } +say [[&add]] 1, 2, 3; #=> 6 + +## * Zip meta-operator +# This one is an infix meta-operator than also can be used as a "normal" operator. +# It takes an optional binary function (by default, it just creates a pair), +# and will pop one value off of each array and call its binary function on these +# until it runs out of elements. It runs the an array with all these new elements. +(1, 2) Z (3, 4); # ((1, 3), (2, 4)), since by default, the function makes an array +1..3 Z+ 4..6; # (5, 7, 9), using the custom infix:<+> function + +# Since `Z` is list-associative (see the list above), +# you can use it on more than one list +(True, False) Z|| (False, False) Z|| (False, False); # (True, False) + +# And, as it turns out, you can also use the reduce meta-operator with it: +[Z||] (True, False), (False, False), (False, False); # (True, False) + + +## And to end the operator list: + +## * Sequence operator # The sequence operator is one of Perl 6's most powerful features: # it's composed of first, on the left, the list you want Perl 6 to deduce from (and might include a closure), # and on the right, a value or the predicate for when to stop, or even Whatever for a lazy infinite list. -- cgit v1.2.3 From 2ff257b551c4a156887b2c630e81f225ea318236 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Tue, 29 Jul 2014 23:42:52 +0200 Subject: Statement prefixes also MAIN. some links under "Going further" TODO start TODO flattening TODO regexps --- perl6.html.markdown | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index b21cb1fa..e2c82a58 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -411,6 +411,7 @@ sub next-index($n) { $n + 1; } my $new-n = next-index(3); # $new-n is now 4 + # This is true for everything, except for the looping constructs (due to performance reasons): # there's no purpose in building a list if we're just going to discard all the results. # If you still want to build one, you can use the `do` prefix: (or the `gather` prefix, which we'll see later) @@ -710,6 +711,10 @@ Foo::Bar::inc; #=> 3 constant Pi = 3.14; constant $var = 1; +# And if you're wondering, yes, it can also contain infinite lists. +constant why-not = 5, 15 ... *; +say why-not[^5]; #=> 5 15 25 35 45 + ## * `state` (happens at run time, but only once) # State variables are only executed one time # (they exist in other langages such as C as `static`) @@ -782,6 +787,62 @@ sub do-db-stuff { UNDO $db.rollback; # or rollback if all hell broke loose } +### Statement prefixes +# Those act a bit like phasers: they affect the behavior of the following code. +# Though, they run in-line with the executable code, so they're in lowercase. +# (`try` and `start` are theoretically in that list, but explained somewhere else) +# Note: all of these (except start) don't need explicit brackets (`{` and `}`) for their block. + +# - `do` (that you already saw) - runs a block or a statement as a term +# You can't normally use a statement as a value (or "term"): +# +# my $value = if True { 1 } # `if` is a statement - parse error +# +# This works: +my $a = do if True { 5 } # with `do`, `if` is now a term. + +# - `once` - Makes sure a piece of code only runs once +for ^5 { once say 1 }; #=> 1 + # Only prints ... once. +# Like `state`, they're cloned per-scope +for ^5 { sub { once say 1 }() } #=> 1 1 1 1 1 + # Prints once per lexical scope + +# - `gather` - Co-routine thread +# Gather allows you to `take` several values in an array, +# much like `do`, but allows you to take any expression. +say gather for ^5 { + take $_ * 3 - 1; + take $_ * 3 + 1; +} #=> -1 1 2 4 5 7 8 10 11 13 +say join ',', gather if False { + take 1; + take 2; + take 3; +} # Doesn't print anything. + +# - `eager` - Evaluate statement eagerly (forces eager context) +# Don't try this at home: +# +# eager 1..*; # this will probably hang for a while (and might crash ...). +# +# But consider: +constant thrice = gather for ^3 { say take $_ }; # Doesn't print anything +# versus: +constant thrice = eager gather for ^3 { say take $_ }; #=> 0 1 2 3 4 + +# - `lazy` - Defer actual evaluation until value is fetched (forces lazy context) +# Not yet implemented !! + +# - `sink` - An `eager` that discards the results (forces sink context) +constant nilthingie = sink for ^3 { .say } #=> 0 1 2 +say nilthingie.perl; #=> Nil + +# - `quietly` - Supresses warnings +# Not yet implemented ! + +# - `contend` - Attempts side effects under STM +# Not yet implemented ! ### More operators thingies ! @@ -1004,4 +1065,37 @@ for { # but the `^` makes it *not run* on the first iteration #=> b c } + +### Extra: the MAIN subroutime +# The `MAIN` subroutine is called when you run a Perl 6 file directly. +# It's very powerful, because Perl 6 actually parses the argument +# and pass them as such to the sub. It also handles named argument (`--foo`) +# and will even go as far as to autogenerate a `--help` +sub MAIN($name) { say "Hello, you !" } +# This produces: +# $ perl6 cli.pl +# Usage: +# t.pl + +# And since it's a regular Perl 6 sub, you can haz multi-dispatch: +# (using a "Bool" for the named argument so that we get `--replace` instead of `--replace=`) +subset File of Str where *.IO.d; # convert to IO object, then check the file exists + +multi MAIN('add', $key, $value, Bool :$replace) { ... } +multi MAIN('remove', $key) { ... } +multi MAIN('import', File, Str :$as) { ... } # omitting parameter name +# This produces: +# $ perl 6 cli.pl +# Usage: +# t.pl [--replace] add +# t.pl remove +# t.pl [--as=] import (File) +# As you can see, this is *very* powerful. It even went as far as to show inline the constants. +# (the type is only displayed if 1. there's no argument name 2. it's a named argument) ``` + +If you want to go further, you can: + - Read the [Perl 6 Advent Calendar](http://perl6advent.wordpress.com/). This is probably the greatest source of Perl 6 information, snippets and such. + - Come along on `#perl6` at `irc.freenode.net`. The folks here are always helpful. + - Check the [source of Perl 6's functions and classes](https://github.com/rakudo/rakudo/tree/nom/src/core). Rakudo is mainly written in Perl 6 (with a lot of NQP, "Not Quite Perl", a Perl 6 subset easier to implement and optimize). + - Read the [Synopses](perlcabal.org/syn). They explain it from an implementor point-of-view, but it's still very interesting. -- cgit v1.2.3 From 10c07f30d50d9cda4b2ad193b212581c0b32152f Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Thu, 31 Jul 2014 23:28:46 +0200 Subject: REGULAAAAAAR EXPREEEESSSIIIIOOOOOONS. GO! TODO nested sub signatures TODO start TODO flattening --- perl6.html.markdown | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index e2c82a58..567c4629 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -1066,6 +1066,134 @@ for { #=> b c } + +### Regular Expressions +# I'm sure a lot of you have been waiting for this one. +# Well, now that you know a good deal of Perl 6 already, we can get started. +# First off, you'll have to forget about "PCRE regexps" (perl-compatible regexps). +# +# IMPORTANT: You may feel like you already know these because you know PCRE. You'd be wrong. +# Some things are the same (like `?`, `+`, and `*`), but sometimes the semantics change (`|`). +# Make sure you read carefully, because you might trip over a new behavior. +# +# Perl 6 has a looot of features related to RegExps. After all, Rakudo parses itself. +# We're first going to look at the syntax itself, then talk about grammars (PEG-like), +# differences between the `token`, `regex` and `rule` keywords, and some more. +# Side note: you still have access to PCRE regexps using the `:P5` modifier. +# (we won't be discussing this in this tutorial, however) +# +# In essence, Perl 6 natively implements PEG ("Parsing Expression Grammars"). +# The pecking order for ambiguous parses is determined by a multi-level tie-breaking test: +# - Longest token matching. `foo\s+` beats `foo` (by 2 or more positions) +# - Longest literal prefix. `food\w*` beats `foo\w*` (by 1) +# - Declaration from most-derived to less derived grammars (grammars are actually classes) +# - Earliest declaration wins +say so 'a' ~~ /a/; #=> True +say so 'a' ~~ / a /; # More readable with some spaces! + +# In all our examples, we're going to use the smart-matching operator against a regexp. +# We're converting the result using `so`, but in fact, it's returning a `Match` object. +# They know how to respond to list indexing, hash indexing (and return the matched string). +# The results of the match are also available as `$/` (implicitly lexically-scoped). +# You can also use the capture variables (`$0`, `$1`, ... - starting at 0, not 1 !). +# +# You can also note that `~~` does not perform start/end checking +# (meaning the regexp can be matched with just one char of the string), +# we're going to explain later how you can do it. + +# In Perl 6, you can have any alphanumeric as a literal, everything else has to be escaped, +# using a backslash or quotes. +say so 'a|b' ~~ / a '|' b /; # `True`. Wouln't mean the same if `|` wasn't escaped +say so 'a|b' ~~ / a \| b /; # `True`. Another way to escape it. + +# The whitespace in a regexp is actually not significant, +# unless you use the `:s` (`:sigspace`, significant space) modifier. +say so 'a b c' ~~ / a b c /; # `False`. Space is not significant here +say so 'a b c' ~~ /:s a b c /; # `True`. We added the modifier `:s` here. + +# It is, however, important as for how modifiers (that you're gonna see just below) +# are applied ... + +## Quantifying - `?`, `+`, `*` and `**`. +# - `?` - 0 or 1 +so 'ac' ~~ / a b c /; # `False` +so 'ac' ~~ / a b? c /; # `True`, the "b" matched 0 times. +so 'abc' ~~ / a b? c /; # `True`, the "b" matched 1 time. + +# ... As you read just before, whitespace is important because it determines +# which part of the regexp is the target of the modifier: +so 'def' ~~ / a b c? /; # `False`. Only the `c` is optional +so 'def' ~~ / ab?c /; # `False`. Whitespace is not significant +so 'def' ~~ / 'abc'? /; # `True`. The whole "abc" group is optional. + +# Here (and below) the quantifier applies only to the `b` + +# - `+` - 1 or more +so 'ac' ~~ / a b+ c /; # `False`; `+` wants at least one matching +so 'abc' ~~ / a b+ c /; # `True`; one is enough +so 'abbbbc' ~~ / a b+ c /; # `True`, matched 4 "b"s + +# - `*` - 0 or more +so 'ac' ~~ / a b* c /; # `True`, they're all optional. +so 'abc' ~~ / a b* c /; # `True` +so 'abbbbc' ~~ / a b* c /; # `True` +so 'aec' ~~ / a b* c /; # `False`. "b"(s) are optional, but can't be something else. + +# - `**` - "Quantify It Yourself". +# If you squint hard enough, you might understand the why exponentation means quantity. +so 'abc' ~~ / a b ** 1 c /; # `True` (exactly one time) +so 'abc' ~~ / a b ** 1..3 c /; # `True` (one to three times) +so 'abbbc' ~~ / a b ** 1..3 c /; # `True` +so 'abbbbbbc' ~~ / a b ** 1..3 c /; # `False` (too much) +so 'abbbbbbc' ~~ / a b ** 3..* c /; # `True` (infinite ranges are okay) + +## Grouping and capturing +# Group: you can group parts of your regexp with `[]`. +# These groups are *not* captured (like PCRE's `(?:)`). +so 'abc' ~~ / a [ b ] c /; # `True`. The grouping does pretty much nothing +so 'fooABCABCbar' ~~ / foo [ A B C ] + bar /; # `True`. + # We match the "abc" 1 or more time. + # (the `+` was applied to the group) + +# But this does not go far enough, because we can't actually get back what we matched. +# Capture: We can actually *capture* the results of the regexp, using parentheses. +so 'fooABCABCbar' ~~ / foo ( A B C ) + bar /; # `True`. (we keep `so` here and use `$/` below) + +# So, starting with the grouping explanations. +# As we said before, our `Match` object is available as `$/`: +say $/; # Will print some weird stuff (we'll explain) (or "Nil" if nothing matched). + +# As we also said before, it has array indexing: +say $/[0]; #=> 「ABC」 「ABC」 + # These weird brackets are `Match` objects. So here, we have an array of that. +say $0; # the same as above. + +# Our capture is `$0` because it's the first and only one capture in the regexp. +# You might be wondering why it's an array, and the answer is simple: +# Some capture (indexed using `$0`, `$/[0]` or a named one) will be an array +# IF it can have more than one element (so, with `*`, `+` and any `**`, but not with `?`). +# Let's use examples to see that: +so 'fooABCbar' ~~ / foo ( A B C )? bar /; # `True` +say $/[0]; #=> 「ABC」 +say $0.WHAT; #=> (Match) + # It can't be more than one, so it's only a single match object. +so 'foobar' ~~ / foo ( A B C )? bar /; #=> True +say $0.WHAT; #=> (Any) + # This capture did not match, so it's empty +so 'foobar' ~~ / foo ( A B C ) ** 0..1 bar /; # `True` +say $0.WHAT; #=> (Array) + # A specific quantifier will always capture an Array, + # may it be a range or a specific value (even 1). + +# If you're wondering how the captures are numbered, here's an explanation: +TODO use graphs from s05 + + +## Alternatives - the `or` of regexps +# WARNING: They are DIFFERENT from PCRE regexps. +so 'abc' ~~ / a [ b | y ] c /; # `True`. Either "b" or "y". +so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviously enough ... + ### Extra: the MAIN subroutime # The `MAIN` subroutine is called when you run a Perl 6 file directly. # It's very powerful, because Perl 6 actually parses the argument -- cgit v1.2.3 From 170de5223165b0f9aa763b171bed153c4073648d Mon Sep 17 00:00:00 2001 From: Patrick Sebastian Zimmermann Date: Tue, 5 Aug 2014 19:50:19 +0200 Subject: Minor fixes to return values. --- perl6.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 567c4629..92219708 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -92,7 +92,7 @@ sub as-many($head, *@rest) { # the `*@` slurpy will basically "take everything e # but not *after*. say @rest.join(' / ') ~ " !"; } -say as-many('Happy', 'Happy', 'Birthday'); #=> Happy Birthday ! +say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday ! # Note that the splat did not consume the parameter before. ## You can call a function with an array using the "argument list flattening" operator `|` @@ -127,7 +127,7 @@ sub with-named($normal-arg, :$named) { } with-named(1, named => 6); #=> 7 # There's one gotcha to be aware of, here: -# If you quote your key, Perl 6 won't be able to see it as compile time, +# If you quote your key, Perl 6 won't be able to see it at compile time, # and you'll have a single Pair object as a positional paramater. with-named(2, :named(5)); #=> 7 @@ -171,9 +171,9 @@ named-def(def => 15); #=> 15 ### Containers # In Perl 6, values are actually stored in "containers". -# the assignment operator asks the container on the left to store the value on its right +# The assignment operator asks the container on the left to store the value on its right. # When passed around, containers are marked as immutable. Which means that, in a function, -# you'll get an error if you try to mutate one of your argument. +# you'll get an error if you try to mutate one of your arguments. # If you really need to, you can ask for a mutable container using `is rw` : sub mutate($n is rw) { $n++; @@ -374,7 +374,7 @@ sub foo(@array [$fst, $snd]) { say "My first is $fst, my second is $snd ! All in all, I'm @array[]."; # (^ remember the `[]` to interpolate the array) } -foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 1 2 +foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 2 3 # If you're not using the array itself, you can also keep it anonymous, much like a scalar: -- cgit v1.2.3 From 0b2f1ff6f1c05a35e0d5fda7dc9e9a7248b44670 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Thu, 14 Aug 2014 21:35:14 +0200 Subject: Update perl6 to fix some of its quirks --- perl6.html.markdown | 534 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 318 insertions(+), 216 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 92219708..fca863af 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -7,24 +7,29 @@ contributors: - ["Nami-Doc", "http://github.com/Nami-Doc"] --- -Perl 6 is a highly capable, feature-rich programming language made for the upcoming hundred years. +Perl 6 is a highly capable, feature-rich programming language made for the +upcoming hundred years. -Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM and [the MoarVM](http://moarvm.com). +Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM +and [the MoarVM](http://moarvm.com). + +Meta-note : the triple pound signs are here to denote headlines, +double paragraphs, and single notes. -Meta-note : the triple pound signs are here to denote headlines, double paragraphs, single notes. `#=>` represents the output of a command. ```perl # Single line comment start with a pound #`( - Multiline comments use #` and a quoting construct. (), [], {}, 「」, etc, will work. + Multiline comments use #` and a quoting construct. + (), [], {}, 「」, etc, will work. ) ### Variables # In Perl 6, you declare a lexical variable using `my` -a +my $variable; # Perl 6 has 4 variable types : ## * Scalars. They represent a single value. They start with a `$` @@ -32,19 +37,22 @@ a my $str = 'String'; my $str2 = "String"; # double quotes allow for interpolation -# variable names can contain but not end with simple quotes and dashes, and can contain (and end with) underscores : +# variable names can contain but not end with simple quotes and dashes, +# and can contain (and end with) underscores : # my $weird'variable-name_ = 5; # works ! my $bool = True; # `True` and `False` are Perl 6's boolean my $inverse = !$bool; # You can invert a bool with the prefix `!` operator -my $forced-bool = so $str; # And you can use the prefix `so` operator which turns its operand into a Bool +my $forced-bool = so $str; # And you can use the prefix `so` operator + # which turns its operand into a Bool ## * Arrays. They represent multiple values. Their name start with `@`. my @array = 1, 2, 3; my @array = 'a', 'b', 'c'; # equivalent to : -my @array = ; # array of words, delimited by space. similar to perl5's qw, or Ruby's %w +my @array = ; # array of words, delimited by space. + # Similar to perl5's qw, or Ruby's %w. say @array[2]; # Array indices start at 0 -- This is the third element @@ -58,10 +66,12 @@ my %hash = 1 => 2, my %hash = autoquoted => "key", # keys *can* get auto-quoted "some other" => "value", # trailing commas are okay ; -my %hash = ; # you can also create a hash from an even-numbered array +my %hash = ; # you can also create a hash + # from an even-numbered array my %hash = key1 => 'value1', key2 => 'value2'; # same as this -# You can also use the "colon pair" syntax: (especially handy for named parameters that you'll see later) +# You can also use the "colon pair" syntax: +# (especially handy for named parameters that you'll see later) my %hash = :w(1), # equivalent to `w => 1` # this is useful for the `True` shortcut: :truey, # equivalent to `:truey(True)`, or `truey => True` @@ -70,33 +80,37 @@ my %hash = :w(1), # equivalent to `w => 1` ; say %hash{'key1'}; # You can use {} to get the value from a key -say %hash; # if it's a string, you can actually use <> +say %hash; # If it's a string, you can actually use <> + # (`{key1}` doesn't work, as Perl6 doesn't have barewords) -## * Subs (subroutines, or functions in most other languages). Stored in variable, they use `&` +## * Subs (subroutines, or functions in most other languages). +# Stored in variable, they use `&`. sub say-hello { say "Hello, world" } -sub say-hello-to(Str $name) { # you can provide the type of an argument - # and it'll be checked at compile-time +sub say-hello-to(Str $name) { # You can provide the type of an argument + # and it'll be checked at compile-time. say "Hello, $name !"; } -# since you can omit parenthesis to call a function with no arguments, -# you need "&" in the name to capture `say-hello` +# Since you can omit parenthesis to call a function with no arguments, +# you need "&" in the name to capture `say-hello`. my &s = &say-hello; -my &other-s = sub { say "anonymous function !" } +my &other-s = sub { say "Anonymous function !" } # A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" -sub as-many($head, *@rest) { # the `*@` slurpy will basically "take everything else". - # Note: you can have parameters *before* (like here) a slurpy one, - # but not *after*. +sub as-many($head, *@rest) { # The `*@` slurpy will basically "take everything else". + # Note: you can have parameters *before* (like here) + # a slurpy one, but not *after*. say @rest.join(' / ') ~ " !"; } say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday ! - # Note that the splat did not consume the parameter before. + # Note that the splat did not consume + # the parameter before. -## You can call a function with an array using the "argument list flattening" operator `|` -# (it's not actually the only feature of the operator, but it's one of them) +## You can call a function with an array using the +# "argument list flattening" operator `|` +# (it's not actually the only role of this operator, but it's one of them) sub concat3($a, $b, $c) { say "$a, $b, $c"; } @@ -105,7 +119,8 @@ concat3(|@array); #=> a, b, c ## It can also have optional arguments: sub with-optional($arg?) { # the "?" marks the argument optional - say "I might return `(Any)` if I don't have an argument passed, or I'll return my argument"; + say "I might return `(Any)` if I don't have an argument passed, + or I'll return my argument"; $arg; } with-optional; # returns Any @@ -132,14 +147,15 @@ with-named(1, named => 6); #=> 7 with-named(2, :named(5)); #=> 7 with-named(3, :4named); #=> 7 - # (special colon pair syntax for numbers, mainly useful for `:2nd` etc) + # (special colon pair syntax for numbers, + # to be used with s// and such, see later) with-named(3); # warns, because we tried to use the undefined $named in a `+`: # by default, named arguments are *optional* # To make a named argument mandatory, you can use `?`'s inverse, `!` sub with-mandatory-named(:$str!) { - say "$named !"; + say "$str !"; } with-mandatory-named(str => "My String"); #=> My String ! with-mandatory-named; # run time error: "Required named parameter not passed" @@ -171,9 +187,10 @@ named-def(def => 15); #=> 15 ### Containers # In Perl 6, values are actually stored in "containers". -# The assignment operator asks the container on the left to store the value on its right. -# When passed around, containers are marked as immutable. Which means that, in a function, -# you'll get an error if you try to mutate one of your arguments. +# The assignment operator asks the container on the left to store the value on +# its right. When passed around, containers are marked as immutable. +# Which means that, in a function, you'll get an error if you try to +# mutate one of your arguments. # If you really need to, you can ask for a mutable container using `is rw` : sub mutate($n is rw) { $n++; @@ -185,7 +202,8 @@ sub mutate($n is rw) { # A sub itself returns a container, which means it can be marked as rw : my $x = 42; sub mod() is rw { $x } -mod() = 52; # in this case, the parentheses are mandatory (else Perl 6 thinks it's a "term") +mod() = 52; # in this case, the parentheses are mandatory + # (else Perl 6 thinks `mod` is a "term") say $x; #=> 52 @@ -197,9 +215,10 @@ say $x; #=> 52 ## Conditionals # - `if` -# Before talking about `if`, we need to know which values are "Truthy" (represent True), -# and which are "Falsey" (or "Falsy") -- meaning they represent False. -# Only these values are Falsey: (), 0, "0", Nil, A type, and of course False itself. +# Before talking about `if`, we need to know which values are "Truthy" +# (represent True), and which are "Falsey" (or "Falsy") -- represent False. +# Only these values are Falsey: (), 0, "0", Nil, A type (like `Str` or `Int`), +# and of course False itself. # Every other value is Truthy. if True { say "It's true !"; @@ -217,18 +236,18 @@ say "Quite truthy" if True; # - Ternary conditional, "?? !!" (like `x ? y : z` in some other languages) my $a = $condition ?? $value-if-true !! $value-if-false; -# - `given`-`when` looks like other languages `switch`, but it's much more powerful thanks to smart matching, -# and thanks to Perl 6's "topic variable", $_. +# - `given`-`when` looks like other languages `switch`, but much more +# powerful thanks to smart matching and thanks to Perl 6's "topic variable", $_. # This variable contains the default argument of a block, # a loop's current iteration (unless explicitly named), etc. -# Given simply puts its argument into `$_` (like a block would do), -# and `when` uses it using the "smart matching" operator. -# Since other Perl 6 constructs use this variable (as said before, like `for`, blocks, etc), -# this means the powerful `when` is not only applicable along with a `given`, -# but instead anywhere a `$_` exists. +# `given` simply puts its argument into `$_` (like a block would do), +# and `when` compares it using the "smart matching" (`~~`) operator. +# Since other Perl 6 constructs use this variable (as said before, like `for`, +# blocks, etc), this means the powerful `when` is not only applicable along with +# a `given`, but instead anywhere a `$_` exists. given "foo bar" { - when /foo/ { # you'll read about the smart-matching operator below -- just know `when` uses it - # this is equivalent to `if $_ ~~ /foo/` + when /foo/ { # You'll read about the smart-matching operator below -- just know `when` uses it. + # This is equivalent to `if $_ ~~ /foo/`. say "Yay !"; } when $_.chars > 50 { # smart matching anything with True (`$a ~~ True`) is True, @@ -242,15 +261,17 @@ given "foo bar" { ## Looping constructs -# - `loop` is an infinite loop if you don't pass it arguments, but can also be a c-style `for` : +# - `loop` is an infinite loop if you don't pass it arguments, +# but can also be a c-style `for` : loop { say "This is an infinite loop !"; last; # last breaks out of the loop, like the `break` keyword in other languages } loop (my $i = 0; $i < 5; $i++) { - next if $i == 3; # `next` skips to the next iteration, like `continue` in other languages. - # Notice that you can also use postfix conditionals, loops, etc. + next if $i == 3; # `next` skips to the next iteration, like `continue` + # in other languages. Note that you can also use postfix conditionals, + # loops, etc. say "This is a C-style for loop !"; } @@ -270,9 +291,10 @@ for @array { } for @array { - next if $_ == 3; # you can skip to the next iteration (like `continue` in C-like languages) - redo if $_ == 4; # you can re-do the iteration, keeping the same topic variable (`$_`) - last if $_ == 5; # you can also break out of a loop (like `break` in C-like languages) + # You can... + next if $_ == 3; # Skip to the next iteration (like `continue` in C-like languages). + redo if $_ == 4; # Re-do the iteration, keeping the same topic variable (`$_`). + last if $_ == 5; # Or break out of a loop (like `break` in C-like languages). } # Note - the "lambda" `->` syntax isn't reserved to `for` : @@ -283,8 +305,8 @@ if long-computation() -> $result { ### Operators ## Since Perl languages are very much operator-based languages -## Perl 6 operators are actually just funny-looking subroutines, in syntactic categories, -## like infix:<+> (addition) or prefix: (bool not) +## Perl 6 operators are actually just funny-looking subroutines, in syntactic +## categories, like infix:<+> (addition) or prefix: (bool not). ## The categories are : # - "prefix" : before (like `!` in `!True`). @@ -312,12 +334,14 @@ if long-computation() -> $result { (1, 2) eqv (1, 3); # - `~~` is smart matching -# for a complete combinations list, use this table : http://perlcabal.org/syn/S03.html#Smart_matching +# For a complete list of combinations, use this table : http://perlcabal.org/syn/S03.html#Smart_matching 'a' ~~ /a/; # true if matches regexp 'key' ~~ %hash; # true if key exists in hash -$arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an argument, returns True -1 ~~ Int; # "is of type" -1 ~~ True; # smart-matching against a boolean always returns that boolean (and will warn). +$arg ~~ &bool-returning-function; # `True` if the function, passed `$arg` + # as an argument, returns `True`. +1 ~~ Int; # "has type" (check superclasses and roles) +1 ~~ True; # smart-matching against a boolean always returns that boolean + # (and will warn). # - `===` is value identity and uses `.WHICH` on the objects to compare them # - `=:=` is container identity and uses `VAR()` on the objects to compare them @@ -330,38 +354,44 @@ $arg ~~ &bool-returning-function; # true if the function, passed `$arg` as an ar 3 .. 7; # 3 to 7, both included # `^` on either side them exclusive on that side : 3 ^..^ 7; # 3 to 7, not included (basically `4 .. 6`) -# this also works as a shortcut for `0..^N` +# This also works as a shortcut for `0..^N`: ^10; # means 0..^10 -# This also allows us to demonstrate that Perl 6 has lazy arrays, using the Whatever Star : +# This also allows us to demonstrate that Perl 6 has lazy arrays, +# using the Whatever Star: my @array = 1..*; # 1 to Infinite ! -say @array[^10]; # you can pass arrays as subscripts and it'll return an array of results - # this will print "1 2 3 4 5 6 7 8 9 10" (and not run out of memory !) -# Note : when reading an infinite list, Perl 6 will "reify" the elements it needs, then keep them in memory -# They won't be calculated more than once. +say @array[^10]; # you can pass arrays as subscripts and it'll return + # an array of results. This will print + # "1 2 3 4 5 6 7 8 9 10" (and not run out of memory !) +# Note : when reading an infinite list, Perl 6 will "reify" the elements +# it needs, then keep them in memory. They won't be calculated more than once. -# Warning, though: if you try this example in the REPL and juste put `1..*`, -# Perl 6 will be forced to try and evaluate the whole array (to print it), -# so you'll end with an infinite loop. +# Warning, though: if you try this example in the REPL and just put `1..*`, +# Perl 6 will be forced to try and evaluate the whole array (to print it), +# so you'll end with an infinite loop. ## * And, Or 3 && 4; # 4, which is Truthy. Calls `.Bool` on `4` and gets `True`. 0 || False; # False. Calls `.Bool` on `0` ## * Short-circuit (and tight) versions of the above -$a && $b && $c; # returns the first argument that evaluates to False, or the last argument +$a && $b && $c; # Returns the first argument that evaluates to False, + # or the last argument. $a || $b; -# And because you're going to want them, you also have composed assignment operators: +# And because you're going to want them, +# you also have composed assignment operators: $a *= 2; # multiply and assignment $b %%= 5; # divisible by and assignment -$c .= say; # method call and assignment +@array .= sort; # calls the `sort` method and assigns the result back ### More on subs ! -# As we said before, Perl 6 has *really* powerful subs. -# We're going to see a few more key concepts that make them better than in any other language :-). +# As we said before, Perl 6 has *really* powerful subs. We're going to see +# a few more key concepts that make them better than in any other language :-). -## Unpacking ! It's the ability to "extract" arrays and keys. It'll work in `my`s and parameters. +## Unpacking ! +# It's the ability to "extract" arrays and keys. +# It'll work in `my`s and in parameter lists. my ($a, $b) = 1, 2; say $a; #=> 1 my ($, $, $c) = 1, 2, 3; # keep the non-interesting anonymous @@ -377,14 +407,17 @@ sub foo(@array [$fst, $snd]) { foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 2 3 -# If you're not using the array itself, you can also keep it anonymous, much like a scalar: +# If you're not using the array itself, you can also keep it anonymous, +# much like a scalar: sub first-of-array(@ [$fst]) { $fst } first-of-array(@small); #=> 1 -first-of-array(@tail); # errors with "Too many positional parameters passed" (the array is too big) +first-of-array(@tail); # Throws an error "Too many positional parameters passed" + # (which means the array is too big). # You can also use a slurp ... sub slurp-in-array(@ [$fst, *@rest]) { # you could decide to keep `*@rest` anonymous - say $fst + @rest.elems; + say $fst + @rest.elems; # `.elems` returns a list's length. + # Here, `@rest` is `(3,)`, since `$fst` holds the `2`. } slurp-in-array(@tail); #=> 3 @@ -403,18 +436,21 @@ sub key-of(% (:value($val), :qua($qua))) { } # Then call it with a hash: (you need to keep the brackets for it to be a hash) -key-of({value => 1}); +key-of({value => 'foo', qua => 1}); #key-of(%hash); # the same (for an equivalent `%hash`) -## The last expression of a sub is returned automatically (though you may use the `return` keyword, of course): +## The last expression of a sub is returned automatically +# (though you may use the `return` keyword, of course): sub next-index($n) { $n + 1; } my $new-n = next-index(3); # $new-n is now 4 -# This is true for everything, except for the looping constructs (due to performance reasons): -# there's no purpose in building a list if we're just going to discard all the results. -# If you still want to build one, you can use the `do` prefix: (or the `gather` prefix, which we'll see later) +# This is true for everything, except for the looping constructs +# (due to performance reasons): there's reason to build a list +# if we're just going to discard all the results. +# If you still want to build one, you can use the `do` statement prefix: +# (or the `gather` prefix, which we'll see later) sub list-of($n) { do for ^$n { # note the use of the range-to prefix operator `^` (`0..^N`) $_ # current loop iteration @@ -424,15 +460,16 @@ my @list3 = list-of(3); #=> (0, 1, 2) ## You can create a lambda with `-> {}` ("pointy block") or `{}` ("block") my &lambda = -> $argument { "The argument passed to this lambda is $argument" } -# `-> {}` and `{}` are pretty much the same thing, except that the former can take arguments, -# and that the latter can be mistaken as a hash by the parser. +# `-> {}` and `{}` are pretty much the same thing, except that the former can +# take arguments, and that the latter can be mistaken as a hash by the parser. # We can, for example, add 3 to each value of an array using map: my @arrayplus3 = map({ $_ + 3 }, @array); # $_ is the implicit argument -# a sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`): -# a block doesn't have a "function context" (though it can have arguments), which means that if you -# return from it, you're going to return from the parent function, compare: +# A sub (`sub {}`) has different semantics than a block (`{}` or `-> {}`): +# A block doesn't have a "function context" (though it can have arguments), +# which means that if you return from it, +# you're going to return from the parent function. Compare: sub is-in(@array, $elem) { # this will `return` out of the `is-in` sub # once the condition evaluated to True, the loop won't be run anymore @@ -441,7 +478,7 @@ sub is-in(@array, $elem) { sub truthy-array(@array) { # this will produce an array of `True` and `False`: # (you can also say `anon sub` for "anonymous subroutine") - map(sub { if $_ { return True } else { return False } }, @array); + map(sub ($i) { if $i { return True } else { return False } }, @array); # ^ the `return` only returns from the anonymous `sub` } @@ -454,15 +491,17 @@ say (*/2)(4); #=> 2 say ((*+3)/5)(5); #=> 1.6 # works even in parens ! -# but if you need to have more than one argument (`$_`) in a block (without wanting to resort to `-> {}`), +# But if you need to have more than one argument (`$_`) +# in a block (without wanting to resort to `-> {}`), # you can also use the implicit argument syntax, `$^` : map({ $^a + $^b + 3 }, @array); # same as the above -# Note : those are sorted lexicographically. `{ $^b / $^a }` is like `-> $a, $b { $b / $a }` +# Note : those are sorted lexicographically. +# `{ $^b / $^a }` is like `-> $a, $b { $b / $a }` ## Multiple Dispatch -# Perl 6 can decide which variant of a `sub` to call based on the type of the arguments, -# or on arbitrary preconditions, like with a type or a `where`: +# Perl 6 can decide which variant of a `sub` to call based on the type of the +# arguments, or on arbitrary preconditions, like with a type or a `where`: # with types multi sub sayit(Int $n) { # note the `multi` keyword here @@ -472,21 +511,25 @@ multi sayit(Str $s) } # the `sub` is the default say "String: $s"; } sayit("foo"); # prints "String: foo" -sayit(True); # fails at *compile time* with "calling 'sayit' will never work with arguments of types ..." +sayit(True); # fails at *compile time* with + # "calling 'sayit' will never work with arguments of types ..." # with arbitrary precondition: multi is-big(Int $n where * > 50) { "Yes !" } # using a closure -multi is-big(Int $ where 10..50) { "Quite." } # this uses smart-matching (could use a regexp, etc) +multi is-big(Int $ where 10..50) { "Quite." } # Using smart-matching + # (could use a regexp, etc) multi is-big(Int $) { "No" } -# you can also name these checks, by creating "subsets": +# You can also name these checks, by creating "subsets": subset Even of Int where * %% 2; -multi odd-or-even(Even) { "Even" } # the main case using the type. We don't name the argument +multi odd-or-even(Even) { "Even" } # The main case using the type. + # We don't name the argument. multi odd-or-even($) { "Odd" } # "else" # You can even dispatch based on a positional's argument presence ! -multi with-or-without-you(:$with!) { # make it mandatory to be able to dispatch against it +multi with-or-without-you(:$with!) { # You need make it mandatory to + # be able to dispatch against it. say "I can live ! Actually, I can't."; } multi with-or-without-you { @@ -494,17 +537,21 @@ multi with-or-without-you { } # This is very, very useful for many purposes, like `MAIN` subs (covered later), # and even the language itself is using it in several places. -# `is`, for example, is actually a `multi sub` named `trait_mod:`, and it works off that. -# `is rw`, for example, is a dispatch to a function with this signature: +# +# - `is`, for example, is actually a `multi sub` named `trait_mod:`, +# and it works off that. +# - `is rw`, is simply a dispatch to a function with this signature: # sub trait_mod:(Routine $r, :$rw!) {} -# (commented because running this would probably lead to some very surprising side-effects !) +# +# (commented because running this would be a terrible idea !) ### Scoping -# In Perl 6, contrarily to many scripting languages (Python, Ruby, PHP, for example), -# you are to declare your variables before using them. You already saw it, with `my`. -# (there are other declarator keywords, like `our`, `has` and `state`, but we'll talk about them later) -# This is called "lexical scoping", where in inner blocks, you can access variables from outer blocks. +# In Perl 6, contrarily to many scripting languages (like Python, Ruby, PHP), +# you are to declare your variables before using them. You know `my`. +# (there are other declarators, `our`, `state`, ..., which we'll see later). +# This is called "lexical scoping", where in inner blocks, +# you can access variables from outer blocks. my $foo = 'Foo'; sub foo { my $bar = 'Bar'; @@ -516,36 +563,39 @@ sub foo { foo()(); #=> 'Foo Bar' # As you can see, `$foo` and `$bar` were captured. -# But if we were to try and use `$bar` outside of `foo`, the variable would be undefined. -# (and you'd get a compile time error) +# But if we were to try and use `$bar` outside of `foo`, +# the variable would be undefined (and you'd get a compile time error). # Perl 6 has another kind of scope : dynamic scope. # They use the twigil (composed sigil) `*` to mark dynamically-scoped variables: my $*a = 1; -# Dyamically-scoped variables depend on the current call stack, instead of the current block stack. +# Dyamically-scoped variables depend on the current call stack, +# instead of the current block depth. sub foo { my $*foo = 1; bar(); # call `bar` in-place } sub bar { say $*foo; # Perl 6 will look into the call stack instead, and find `foo`'s `$*a`, - # even though the blocks aren't nested (they're call-nested). + # even though the blocks aren't nested (they're call-nested). #=> 1 } ### Object Model ## Perl 6 has a quite comprehensive object model -## You declare a class with the keyword `class`, fields with `has`, methods with `method`. -## In Perl 6, every field is private, and named `$!attr`, but if you declare it with `$.`, -## you get a public (immutable) accessor along with it. +# You declare a class with the keyword `class`, fields with `has`, +# methods with `method`. Every field to private, and is named `$!attr`, +# but you have `$.` to get a public (immutable) accessor along with it. +# (using `$.` is like using `$!` plus a `method` with the same name) # (Perl 6's object model ("SixModel") is very flexible, and allows you to dynamically add methods, # change semantics, etc -- This will not be covered here, and you should refer to the Synopsis) class A { - has $.field; # `$.field` is immutable. Use `$!field` from inside the class to modify it. - has $.other-field is rw; # You can, however, mark a public field as being read/write. + has $.field; # `$.field` is immutable. + # From inside the class, use `$!field` to modify it. + has $.other-field is rw; # You can obviously mark a public field `rw`. has Int $!private-field = 10; method get-value { @@ -556,7 +606,7 @@ class A { # $.field = $n; # As stated before, you can't use the `$.` immutable version. $!field = $n; # This works, because `$!` is always mutable. - $.other-field = 5; # This works, because `$.other-field` was declared `rw` (mutable). + $.other-field = 5; # This works, because `$.other-field` is `rw`. } method !private-method { @@ -565,13 +615,15 @@ class A { }; # Create a new instance of A with $.field set to 5 : -# note : you can't set private-field from here (more later on) +# Note: you can't set private-field from here (more later on). my $a = A.new(field => 5); $a.get-value; #=> 18 #$a.field = 5; # This fails, because the `has $.field` is immutable -$a.other-field = 10; # This, however, works, because the public field is mutable (`rw`). +$a.other-field = 10; # This, however, works, because the public field + # is mutable (`rw`). -## Perl 6 also has inheritance (along with multiple inheritance ... Considered a misfeature by many) +## Perl 6 also has inheritance (along with multiple inheritance) +# (though considered a misfeature by many) class A { has $.val; @@ -591,12 +643,14 @@ class B is A { # inheritance uses `is` method bar { $.val * 10 } # this shadows A's `bar` } -my B $b .= new(val => 5); # When you use `my T $var`, `$var` starts off with `T` itself in it, - # so you can call `new` on it. - # (`.=` is just the compound operator composed of the dot-call and of the assignment operator - # `$a .= b` is the same as `$a = $a.b`) - # Also note that `BUILD` (the method called inside `new`) will set parent properties too, - # so you can pass `val => 5` +# When you use `my T $var`, `$var` starts off with `T` itself in it, +# so you can call `new` on it. +# (`.=` is just the dot-call and the assignment operator: +# `$a .= b` is the same as `$a = $a.b`) +# Also note that `BUILD` (the method called inside `new`) +# will set parent properties too, so you can pass `val => 5`. +my B $b .= new(val => 5); + # $b.not-inherited; # This won't work, for reasons explained above $b.foo; # prints 5 $b.bar; #=> 50, since it calls B's `bar` @@ -613,27 +667,30 @@ role PrintableVal { class Item does PrintableVal { has $.val; - # When `does`-ed, a `role` literally "mixes in" the class : - # the methods and fields are put together, which means a class can access - # the private fields/methods of its roles (but not the inverse !) : + # When `does`-ed, a `role` literally "mixes in" the class: + # the methods and fields are put together, which means a class can access + # the private fields/methods of its roles (but not the inverse !): method access { say $!counter++; } - # However, this : + # However, this: # method print {} - # is an error, since the compiler wouldn't know which `print` to use : - # contrarily to inheritance, methods mixed in can't be shadowed - they're put at the same "level" + # is ONLY valid when `print` isn't a `multi` with the same dispatch. + # (this means a parent class can shadow a child class's `multi print() {}`, + # but it's an error if a role does) - # NOTE: You can use a role as a class (with `is ROLE`). In this case, methods will be shadowed, - # since the compiler will consider `ROLE` to be a class + # NOTE: You can use a role as a class (with `is ROLE`). In this case, methods + # will be shadowed, since the compiler will consider `ROLE` to be a class. } ### Exceptions # Exceptions are built on top of classes, usually in the package `X` (like `X::IO`). -# Unlike many other languages, in Perl 6, you put the `CATCH` block *within* the block to `try`. -# By default, a `try` has a `CATCH` block that catches any exception (`CATCH { default {} }`). -# You can redefine it using `when`s (and `default`) to handle the exceptions you want: +# Unlike many other languages, in Perl 6, you put the `CATCH` block *within* the +# block to `try`. By default, a `try` has a `CATCH` block that catches +# any exception (`CATCH { default {} }`). +# You can redefine it using `when`s (and `default`) +# to handle the exceptions you want: try { open 'foo'; CATCH { @@ -649,17 +706,17 @@ die X::AdHoc.new(payload => 'Error !'); # TODO CONTROL ### Packages -# Packages are a way to reuse code. Packages are like "namespaces", and any element of the six model -# (`module`, `role`, `class`, `grammar`, `subset` and `enum`) are actually packages. -# (you can say that packages are the lowest common denomitor between them) -# Packages play a big part in a language, as Perl is well-known for CPAN, +# Packages are a way to reuse code. Packages are like "namespaces", and any +# element of the six model (`module`, `role`, `class`, `grammar`, `subset` +# and `enum`) are actually packages. (Packages are the lowest common denomitor) +# Packages play a big part in a language, especially as Perl is well-known for CPAN, # the Comprehensive Perl Archive Network. -# You usually don't use packages directly : you use `class Package::Name::Here;`, or if you -# only want to export variables/subs, you can use `module`: -module Hello::World { # bracketed form - # if `Hello` doesn't exist yet, it'll just be created as an "empty package stub" - # that can be redeclared as something else later. - # declarations here +# You usually don't use packages directly: you use `class Package::Name::Here;`, +# or if you only want to export variables/subs, you can use `module`: +module Hello::World { # Bracketed form + # If `Hello` doesn't exist yet, it'll just be a "stub", + # that can be redeclared as something else later. + # ... declarations here ... } module Parse::Text; # file-scoped form grammar Parse::Text::Grammar { # A grammar is a fine package, which you could `use` @@ -692,7 +749,8 @@ my $actions = JSON::Tiny::Actions.new; module Foo::Bar { our $n = 1; # note: you can't put a type constraint on an `our` variable our sub inc { - our sub available { # if you try to make scoped `sub`s `our` ... Better know what you're doing (Don't !). + our sub available { # If you try to make inner `sub`s `our`... + # Better know what you're doing (Don't !). say "Don't do that. Seriously. You'd get burned."; } my sub unavailable { # `my sub` is the default @@ -725,23 +783,24 @@ sub fixed-rand { fixed-rand for ^10; # will print the same number 10 times # Note, however, that they exist separately in different enclosing contexts. -# If you declare a function with a `state` within a loop, it'll re-create the variable -# for each iteration of loop. See: +# If you declare a function with a `state` within a loop, it'll re-create the +# variable for each iteration of the loop. See: for ^5 -> $a { sub foo { state $val = rand; # This will be a different value for every value of `$a` } for ^5 -> $b { - say foo; # This will print the same value 5 times, but only 5. Next iteration will re-run `rand` + say foo; # This will print the same value 5 times, but only 5. + # Next iteration will re-run `rand`. } } ### Phasers -# Phasers in Perl 6 are blocks that happen at determined points of time in your program -# When the program is compiled, when a for loop runs, when you leave a block, when -# an exception gets thrown ... (`CATCH` is actually a phaser !) +# Phasers in Perl 6 are blocks that happen at determined points of time in your +# program. When the program is compiled, when a for loop runs, when you leave a +# block, when an exception gets thrown ... (`CATCH` is actually a phaser !) # Some of them can be used for their return values, some of them can't # (those that can have a "[*]" in the beginning of their explanation text). # Let's have a look ! @@ -791,7 +850,7 @@ sub do-db-stuff { # Those act a bit like phasers: they affect the behavior of the following code. # Though, they run in-line with the executable code, so they're in lowercase. # (`try` and `start` are theoretically in that list, but explained somewhere else) -# Note: all of these (except start) don't need explicit brackets (`{` and `}`) for their block. +# Note: all of these (except start) don't need explicit brackets `{` and `}`. # - `do` (that you already saw) - runs a block or a statement as a term # You can't normally use a statement as a value (or "term"): @@ -848,8 +907,9 @@ say nilthingie.perl; #=> Nil ## Everybody loves operators ! Let's get more of them -## The precedence list can be found here : http://perlcabal.org/syn/S03.html#Operator_precedence -## But first, we need a little explanation about associativity : +# The precedence list can be found here: +# http://perlcabal.org/syn/S03.html#Operator_precedence +# But first, we need a little explanation about associativity: # * Binary operators: $a ! $b ! $c; # with a left-associative `!`, this is `($a ! $b) ! $c` @@ -864,8 +924,9 @@ $a ! $b ! $c; # with a list-associative `!`, this is `infix:<>` !$a! # with non-associative `!`, this is illegal ## Create your own operators ! -# Okay, you've been reading all of that, so I guess I should try to show you something exciting. -# I'll tell you a little secret (actually not): +# Okay, you've been reading all of that, so I guess I should try +# to show you something exciting. +# I'll tell you a little secret (or not-so-secret): # In Perl 6, all operators are actually just funny-looking subroutines. # You can declare an operator just like you declare a sub: @@ -906,36 +967,46 @@ sub circumfix:<[ ]>(Int $n) { say [5]; #=> 3125 # circumfix is around. Again, not whitespace. -sub postcircumfix:<{ }>(Str $s, Int $idx) { # post-circumfix is "after a term, around something" +sub postcircumfix:<{ }>(Str $s, Int $idx) { + # post-circumfix is + # "after a term, around something" $s.substr($idx, 1); } say "abc"{1}; #=> b # after the term `"abc"`, and around the index (1) # This really means a lot -- because everything in Perl 6 uses this. -# For example, to delete a key from a hash, you use the `:delete` adverb (named argument) +# For example, to delete a key from a hash, you use the `:delete` adverb +# (a simple named argument underneath): %h{$key}:delete; # equivalent to: -postcircumfix:<{ }>(%h, $key, :delete); -# It's *all* using the same building blocks! Syntactic categories (prefix infix ...), -# named arguments (adverbs), ..., used to build the language are available to you. +postcircumfix:<{ }>(%h, $key, :delete); # (you can call operators like that) +# It's *all* using the same building blocks! +# Syntactic categories (prefix infix ...), named arguments (adverbs), ..., +# - used to build the language - are available to you. -# (you are, obviously, recommended against making an operator out of *everything* -- -# with great power comes great responsibility) +# (you are, obviously, recommended against making an operator out of +# *everything* -- with great power comes great responsibility) ## Meta operators ! -# Oh boy, get ready. Get ready, because we're dwelving deep into the rabbit's hole, -# and you probably won't want to go back to other languages after reading that. +# Oh boy, get ready. Get ready, because we're dwelving deep +# into the rabbit's hole, and you probably won't want to go +# back to other languages after reading that. # (I'm guessing you don't want to already at that point). # Meta-operators, as their name suggests, are *composed* operators. # Basically, they're operators that apply another operator. ## * Reduce meta-operator -# It's a prefix meta-operator that takes a binary functions and one or many lists. -# If it doesn't get passed any argument, it either return a "default value" for this operator -# (a value that'd be non-meaningful if contained in a list) or `Any` if there's none. +# It's a prefix meta-operator that takes a binary functions and +# one or many lists. If it doesn't get passed any argument, +# it either return a "default value" for this operator +# (a value that wouldn't change the result if passed as one +# of the element of the list to be passed to the operator), +# or `Any` if there's none (examples below). +# # Otherwise, it pops an element from the list(s) one at a time, and applies the binary function # to the last result (or the list's first element) and the popped element. +# # To sum a list, you could use the reduce meta-operator with `+`, i.e.: say [+] 1, 2, 3; #=> 6 # equivalent to `(1+2)+3` @@ -943,18 +1014,20 @@ say [*] 1..5; #=> 120 # equivalent to `((((1*2)*3)*4)*5)`. # You can reduce with any operator, not just with mathematical ones. -# For example, you could reduce with `//` to get the first defined element of a list: +# For example, you could reduce with `//` to get +# the first defined element of a list: say [//] Nil, Any, False, 1, 5; #=> False # (Falsey, but still defined) # Default value examples: -say [*] (); #=> 1 -say [+] (); #=> 0 - # In both cases, they're results that, if they were contained in the lists, - # wouldn't have any impact on the final value (since N*1=N and N+0=N). +say [*] (); #=> 1 +say [+] (); #=> 0 + # In both cases, they're results that, were they in the lists, + # wouldn't have any impact on the final value + # (since N*1=N and N+0=N). say [//]; #=> (Any) - # There's no "default value" for `//` + # There's no "default value" for `//`. # You can also call it with a function you made up, using double brackets: sub add($a, $b) { $a + $b } @@ -980,23 +1053,31 @@ say [[&add]] 1, 2, 3; #=> 6 ## * Sequence operator # The sequence operator is one of Perl 6's most powerful features: -# it's composed of first, on the left, the list you want Perl 6 to deduce from (and might include a closure), -# and on the right, a value or the predicate for when to stop, or even Whatever for a lazy infinite list. +# it's composed of first, on the left, the list you want Perl 6 to deduce from +# (and might include a closure), and on the right, a value or the predicate +# that says when to stop (or Whatever for a lazy infinite list). my @list = 1, 2, 3 ... 10; # basic deducing -#my @list = 1, 3, 6 ... 10; # this throws you into an infinite loop, because Perl 6 can't figure out the end -my @list = 1, 2, 3 ...^ 10; # as with ranges, you can exclude the last element (when the predicate matches) -my @list = 1, 3, 9 ... * > 30; # you can use a predicate (with the Whatever Star, here) +#my @list = 1, 3, 6 ... 10; # this throws you into an infinite loop, + # because Perl 6 can't figure out the end +my @list = 1, 2, 3 ...^ 10; # as with ranges, you can exclude the last element + # (the iteration when the predicate matches). +my @list = 1, 3, 9 ... * > 30; # you can use a predicate + # (with the Whatever Star, here). my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalent to the above) -my @fib = 1, 1, *+* ... *; # lazy infinite list of prime numbers, computed using a closure ! + +my @fib = 1, 1, *+* ... *; # lazy infinite list of prime numbers, + # computed using a closure! my @fib = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above) say @fib[^10]; #=> 1 1 2 3 5 8 13 21 34 55 # (using a range as the index) # Note : as for ranges, once reified, elements aren't re-calculated. -# That's why `@primes[^100]` will take a long time the first time you print it, then be instant +# That's why `@primes[^100]` will take a long time the first time you print +# it, then be instant. ## * Sort comparison -# They return one value of the `Order` enum : `Less`, `Same` and `More` (which numerify to -1, 0 or +1). +# They return one value of the `Order` enum : `Less`, `Same` and `More` +# (which numerify to -1, 0 or +1). 1 <=> 4; # sort comparison for numerics 'a' leg 'b'; # sort comparison for string $obj eqv $obj2; # sort comparison using eqv semantics @@ -1014,14 +1095,17 @@ say Any // Nil // 0 // 5; #=> 5 say True ^^ False; #=> True ## * Flip Flop -# The flip flop operators (`ff` and `fff`, equivalent to Perl 5/Ruby's `..` and `...`). +# The flip flop operators (`ff` and `fff`, equivalent to P5's `..`/`...`). # are operators that take two predicates to test: -# They are `False` until their left side returns `True`, then are `True` until their right side returns `True`. -# Like for ranges, you can exclude the iteration when it became `True`/`False` by using `^` on either side. +# They are `False` until their left side returns `True`, then are `True` until +# their right side returns `True`. +# Like for ranges, you can exclude the iteration when it became `True`/`False` +# by using `^` on either side. # Let's start with an example : for { # by default, `ff`/`fff` smart-match (`~~`) against `$_`: - if 'met' ^ff 'meet' { # won't enter the if for "met" (explained in details below). + if 'met' ^ff 'meet' { # Won't enter the if for "met" + # (explained in details below). .say } @@ -1031,20 +1115,24 @@ for { } # This will print "young hero we shall meet" (excluding "met"): # the flip-flop will start returning `True` when it first encounters "met" -# (but will still return `False` for "met" itself, due to the leading `^` on `ff`), -# until it sees "meet", which is when it'll start returning `False`. +# (but will still return `False` for "met" itself, due to the leading `^` +# on `ff`), until it sees "meet", which is when it'll start returning `False`. # The difference between `ff` (awk-style) and `fff` (sed-style) is that -# `ff` will test its right side just as its left side changes to `True`, -# and can get back to `False` right away (*except* it'll be `True` for the iteration that matched) -# while `fff` will wait for the next iteration to try its right side, once its left side changed: +# `ff` will test its right side right when its left side changes to `True`, +# and can get back to `False` right away +# (*except* it'll be `True` for the iteration that matched) - +# While `fff` will wait for the next iteration to +# try its right side, once its left side changed: .say if 'B' ff 'B' for ; #=> B B - # because the right-hand-side was tested directly (and returned `True`). + # because the right-hand-side was tested + # directly (and returned `True`). # "B"s are still printed since it matched that time - # (it just went back to `False` right away) + # (it just went back to `False` right away). .say if 'B' fff 'B' for ; #=> B C B - # because the right-hand-side wasn't tested until `$_` became "C" - # (and thus did not match directly). + # because the right-hand-side wasn't tested until + # `$_` became "C" + # (and thus did not match instantly). # A flip-flop can change state as many times as needed: for { @@ -1054,12 +1142,15 @@ for 50 ff *; # Once the flip-flop reaches a number greater than 50, it'll never go back to `False` +for (1, 3, 60, 3, 40, 60) { # Note: the parenthesis are superfluous here + # (sometimes called "superstitious parentheses") + .say if $_ > 50 ff *; # Once the flip-flop reaches a number greater than 50, + # it'll never go back to `False` #=> 60 3 40 60 } -# You can also use this property to create an `If` that'll not execute the first time : +# You can also use this property to create an `If` +# that'll not go through the first time : for { .say if * ^ff *; # the flip-flop is `True` and never goes back to `False`, # but the `^` makes it *not run* on the first iteration @@ -1072,28 +1163,33 @@ for { # Well, now that you know a good deal of Perl 6 already, we can get started. # First off, you'll have to forget about "PCRE regexps" (perl-compatible regexps). # -# IMPORTANT: You may feel like you already know these because you know PCRE. You'd be wrong. -# Some things are the same (like `?`, `+`, and `*`), but sometimes the semantics change (`|`). +# IMPORTANT: Don't skip them because you know PCRE. They're different. +# Some things are the same (like `?`, `+`, and `*`), +# but sometimes the semantics change (`|`). # Make sure you read carefully, because you might trip over a new behavior. # -# Perl 6 has a looot of features related to RegExps. After all, Rakudo parses itself. -# We're first going to look at the syntax itself, then talk about grammars (PEG-like), -# differences between the `token`, `regex` and `rule` keywords, and some more. +# Perl 6 has many features related to RegExps. After all, Rakudo parses itself. +# We're first going to look at the syntax itself, +# then talk about grammars (PEG-like), differences between +# `token`, `regex` and `rule` declarators, and some more. # Side note: you still have access to PCRE regexps using the `:P5` modifier. # (we won't be discussing this in this tutorial, however) # # In essence, Perl 6 natively implements PEG ("Parsing Expression Grammars"). -# The pecking order for ambiguous parses is determined by a multi-level tie-breaking test: +# The pecking order for ambiguous parses is determined by a multi-level +# tie-breaking test: # - Longest token matching. `foo\s+` beats `foo` (by 2 or more positions) # - Longest literal prefix. `food\w*` beats `foo\w*` (by 1) -# - Declaration from most-derived to less derived grammars (grammars are actually classes) +# - Declaration from most-derived to less derived grammars +# (grammars are actually classes) # - Earliest declaration wins say so 'a' ~~ /a/; #=> True say so 'a' ~~ / a /; # More readable with some spaces! -# In all our examples, we're going to use the smart-matching operator against a regexp. -# We're converting the result using `so`, but in fact, it's returning a `Match` object. -# They know how to respond to list indexing, hash indexing (and return the matched string). +# In all our examples, we're going to use the smart-matching operator against +# a regexp. We're converting the result using `so`, but in fact, it's +# returning a `Match` object. They know how to respond to list indexing, +# hash indexing, and return the matched string. # The results of the match are also available as `$/` (implicitly lexically-scoped). # You can also use the capture variables (`$0`, `$1`, ... - starting at 0, not 1 !). # @@ -1101,8 +1197,8 @@ say so 'a' ~~ / a /; # More readable with some spaces! # (meaning the regexp can be matched with just one char of the string), # we're going to explain later how you can do it. -# In Perl 6, you can have any alphanumeric as a literal, everything else has to be escaped, -# using a backslash or quotes. +# In Perl 6, you can have any alphanumeric as a literal, +# everything else has to be escaped, using a backslash or quotes. say so 'a|b' ~~ / a '|' b /; # `True`. Wouln't mean the same if `|` wasn't escaped say so 'a|b' ~~ / a \| b /; # `True`. Another way to escape it. @@ -1140,7 +1236,8 @@ so 'abbbbc' ~~ / a b* c /; # `True` so 'aec' ~~ / a b* c /; # `False`. "b"(s) are optional, but can't be something else. # - `**` - "Quantify It Yourself". -# If you squint hard enough, you might understand the why exponentation means quantity. +# If you squint hard enough, you might understand +# why exponentation is used for quantity. so 'abc' ~~ / a b ** 1 c /; # `True` (exactly one time) so 'abc' ~~ / a b ** 1..3 c /; # `True` (one to three times) so 'abbbc' ~~ / a b ** 1..3 c /; # `True` @@ -1151,11 +1248,12 @@ so 'abbbbbbc' ~~ / a b ** 3..* c /; # `True` (infinite ranges are okay) # Group: you can group parts of your regexp with `[]`. # These groups are *not* captured (like PCRE's `(?:)`). so 'abc' ~~ / a [ b ] c /; # `True`. The grouping does pretty much nothing -so 'fooABCABCbar' ~~ / foo [ A B C ] + bar /; # `True`. - # We match the "abc" 1 or more time. - # (the `+` was applied to the group) +so 'fooABCABCbar' ~~ / foo [ A B C ] + bar /; +# The previous line returns `True`. +# We match the "abc" 1 or more time (the `+` was applied to the group). -# But this does not go far enough, because we can't actually get back what we matched. +# But this does not go far enough, because we can't actually get back what +# we matched. # Capture: We can actually *capture* the results of the regexp, using parentheses. so 'fooABCABCbar' ~~ / foo ( A B C ) + bar /; # `True`. (we keep `so` here and use `$/` below) @@ -1165,13 +1263,15 @@ say $/; # Will print some weird stuff (we'll explain) (or "Nil" if nothing match # As we also said before, it has array indexing: say $/[0]; #=> 「ABC」 「ABC」 - # These weird brackets are `Match` objects. So here, we have an array of that. -say $0; # the same as above. + # These weird brackets are `Match` objects. + # Here, we have an array of these. +say $0; # The same as above. # Our capture is `$0` because it's the first and only one capture in the regexp. # You might be wondering why it's an array, and the answer is simple: # Some capture (indexed using `$0`, `$/[0]` or a named one) will be an array -# IF it can have more than one element (so, with `*`, `+` and any `**`, but not with `?`). +# IFF it can have more than one element +# (so, with `*`, `+` and any `**`, but not with `?`). # Let's use examples to see that: so 'fooABCbar' ~~ / foo ( A B C )? bar /; # `True` say $/[0]; #=> 「ABC」 @@ -1206,7 +1306,8 @@ sub MAIN($name) { say "Hello, you !" } # t.pl # And since it's a regular Perl 6 sub, you can haz multi-dispatch: -# (using a "Bool" for the named argument so that we get `--replace` instead of `--replace=`) +# (using a "Bool" for the named argument so that we get `--replace` +# instead of `--replace=1`) subset File of Str where *.IO.d; # convert to IO object, then check the file exists multi MAIN('add', $key, $value, Bool :$replace) { ... } @@ -1218,8 +1319,9 @@ multi MAIN('import', File, Str :$as) { ... } # omitting parameter name # t.pl [--replace] add # t.pl remove # t.pl [--as=] import (File) -# As you can see, this is *very* powerful. It even went as far as to show inline the constants. -# (the type is only displayed if 1. there's no argument name 2. it's a named argument) +# As you can see, this is *very* powerful. +# It even went as far as to show inline the constants. +# (the type is only displayed if the argument is `$`/is named) ``` If you want to go further, you can: -- cgit v1.2.3 From 1828ef068dd224a8402a4c786569de985f3793b2 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Thu, 21 Aug 2014 00:34:39 +0200 Subject: Make the text fit. --- perl6.html.markdown | 84 ++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 39 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index fca863af..4ab90914 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -30,7 +30,7 @@ double paragraphs, and single notes. # In Perl 6, you declare a lexical variable using `my` my $variable; -# Perl 6 has 4 variable types : +# Perl 6 has 4 kinds of variables: ## * Scalars. They represent a single value. They start with a `$` @@ -56,7 +56,8 @@ my @array = ; # array of words, delimited by space. say @array[2]; # Array indices start at 0 -- This is the third element -say "Interpolate an array using [] : @array[]"; #=> Interpolate an array using [] : a b c +say "Interpolate an array using [] : @array[]"; +#=> Interpolate an array using [] : a b c ## * Hashes. Key-Value Pairs. # Hashes are actually arrays of Pairs (`Key => Value`), @@ -99,7 +100,7 @@ my &s = &say-hello; my &other-s = sub { say "Anonymous function !" } # A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" -sub as-many($head, *@rest) { # The `*@` slurpy will basically "take everything else". +sub as-many($head, *@rest) { # `*@` (slurpy) will basically "take everything else". # Note: you can have parameters *before* (like here) # a slurpy one, but not *after*. say @rest.join(' / ') ~ " !"; @@ -191,7 +192,7 @@ named-def(def => 15); #=> 15 # its right. When passed around, containers are marked as immutable. # Which means that, in a function, you'll get an error if you try to # mutate one of your arguments. -# If you really need to, you can ask for a mutable container using `is rw` : +# If you really need to, you can ask for a mutable container using `is rw`: sub mutate($n is rw) { $n++; say "\$n is now $n !"; @@ -199,7 +200,7 @@ sub mutate($n is rw) { # If what you want is a copy instead, use `is copy`. -# A sub itself returns a container, which means it can be marked as rw : +# A sub itself returns a container, which means it can be marked as rw: my $x = 42; sub mod() is rw { $x } mod() = 52; # in this case, the parentheses are mandatory @@ -210,7 +211,7 @@ say $x; #=> 52 ### Control Flow Structures # You don't need to put parenthesis around the condition, -# but that also means you always have to use brackets (`{ }`) for their body : +# but that also means you always have to use brackets (`{ }`) for their body: ## Conditionals @@ -246,7 +247,7 @@ my $a = $condition ?? $value-if-true !! $value-if-false; # blocks, etc), this means the powerful `when` is not only applicable along with # a `given`, but instead anywhere a `$_` exists. given "foo bar" { - when /foo/ { # You'll read about the smart-matching operator below -- just know `when` uses it. + when /foo/ { # Don't worry about smart matching -- just know `when` uses it. # This is equivalent to `if $_ ~~ /foo/`. say "Yay !"; } @@ -262,7 +263,7 @@ given "foo bar" { ## Looping constructs # - `loop` is an infinite loop if you don't pass it arguments, -# but can also be a c-style `for` : +# but can also be a c-style `for`: loop { say "This is an infinite loop !"; last; # last breaks out of the loop, like the `break` keyword in other languages @@ -270,8 +271,8 @@ loop { loop (my $i = 0; $i < 5; $i++) { next if $i == 3; # `next` skips to the next iteration, like `continue` - # in other languages. Note that you can also use postfix conditionals, - # loops, etc. + # in other languages. Note that you can also use postfix + # conditionals, loops, etc. say "This is a C-style for loop !"; } @@ -292,12 +293,12 @@ for @array { for @array { # You can... - next if $_ == 3; # Skip to the next iteration (like `continue` in C-like languages). + next if $_ == 3; # Skip to the next iteration (`continue` in C-like languages). redo if $_ == 4; # Re-do the iteration, keeping the same topic variable (`$_`). last if $_ == 5; # Or break out of a loop (like `break` in C-like languages). } -# Note - the "lambda" `->` syntax isn't reserved to `for` : +# Note - the "lambda" `->` syntax isn't reserved to `for`: if long-computation() -> $result { say "The result is $result"; } @@ -308,12 +309,12 @@ if long-computation() -> $result { ## Perl 6 operators are actually just funny-looking subroutines, in syntactic ## categories, like infix:<+> (addition) or prefix: (bool not). -## The categories are : -# - "prefix" : before (like `!` in `!True`). -# - "postfix" : after (like `++` in `$a++`). -# - "infix" : in between (like `*` in `4 * 3`). -# - "circumfix" : around (like `[`-`]` in `[1, 2]`). -# - "post-circumfix" : around, after another term (like `{`-`}` in `%hash{'key'}`) +## The categories are: +# - "prefix": before (like `!` in `!True`). +# - "postfix": after (like `++` in `$a++`). +# - "infix": in between (like `*` in `4 * 3`). +# - "circumfix": around (like `[`-`]` in `[1, 2]`). +# - "post-circumfix": around, after another term (like `{`-`}` in `%hash{'key'}`) ## The associativity and precedence list are explained below. @@ -334,7 +335,8 @@ if long-computation() -> $result { (1, 2) eqv (1, 3); # - `~~` is smart matching -# For a complete list of combinations, use this table : http://perlcabal.org/syn/S03.html#Smart_matching +# For a complete list of combinations, use this table: +# http://perlcabal.org/syn/S03.html#Smart_matching 'a' ~~ /a/; # true if matches regexp 'key' ~~ %hash; # true if key exists in hash $arg ~~ &bool-returning-function; # `True` if the function, passed `$arg` @@ -415,7 +417,7 @@ first-of-array(@tail); # Throws an error "Too many positional parameters passed" # (which means the array is too big). # You can also use a slurp ... -sub slurp-in-array(@ [$fst, *@rest]) { # you could decide to keep `*@rest` anonymous +sub slurp-in-array(@ [$fst, *@rest]) { # You could keep `*@rest` anonymous say $fst + @rest.elems; # `.elems` returns a list's length. # Here, `@rest` is `(3,)`, since `$fst` holds the `2`. } @@ -485,7 +487,7 @@ sub truthy-array(@array) { # You can also use the "whatever star" to create an anonymous function # (it'll stop at the furthest operator in the current expression) my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` -my @arrayplus3 = map(*+*+3, @array); # also works. Same as `-> $a, $b { $a + $b + 3 }` +my @arrayplus3 = map(*+*+3, @array); # Same as `-> $a, $b { $a + $b + 3 }` say (*/2)(4); #=> 2 # Immediatly execute the function Whatever created. say ((*+3)/5)(5); #=> 1.6 @@ -576,7 +578,7 @@ sub foo { bar(); # call `bar` in-place } sub bar { - say $*foo; # Perl 6 will look into the call stack instead, and find `foo`'s `$*a`, + say $*foo; # `$*a` will be looked in the call stack, and find `foo`'s, # even though the blocks aren't nested (they're call-nested). #=> 1 } @@ -589,8 +591,9 @@ sub bar { # but you have `$.` to get a public (immutable) accessor along with it. # (using `$.` is like using `$!` plus a `method` with the same name) -# (Perl 6's object model ("SixModel") is very flexible, and allows you to dynamically add methods, -# change semantics, etc -- This will not be covered here, and you should refer to the Synopsis) +# (Perl 6's object model ("SixModel") is very flexible, +# and allows you to dynamically add methods, change semantics, etc ... +# (this will not be covered here, and you should refer to the Synopsis). class A { has $.field; # `$.field` is immutable. @@ -685,7 +688,7 @@ class Item does PrintableVal { } ### Exceptions -# Exceptions are built on top of classes, usually in the package `X` (like `X::IO`). +# Exceptions are built on top of classes, in the package `X` (like `X::IO`). # Unlike many other languages, in Perl 6, you put the `CATCH` block *within* the # block to `try`. By default, a `try` has a `CATCH` block that catches # any exception (`CATCH { default {} }`). @@ -709,7 +712,7 @@ die X::AdHoc.new(payload => 'Error !'); # Packages are a way to reuse code. Packages are like "namespaces", and any # element of the six model (`module`, `role`, `class`, `grammar`, `subset` # and `enum`) are actually packages. (Packages are the lowest common denomitor) -# Packages play a big part in a language, especially as Perl is well-known for CPAN, +# Packages are important - especially as Perl is well-known for CPAN, # the Comprehensive Perl Archive Network. # You usually don't use packages directly: you use `class Package::Name::Here;`, # or if you only want to export variables/subs, you can use `module`: @@ -719,7 +722,7 @@ module Hello::World { # Bracketed form # ... declarations here ... } module Parse::Text; # file-scoped form -grammar Parse::Text::Grammar { # A grammar is a fine package, which you could `use` +grammar Parse::Text::Grammar { # A grammar is a package, which you could `use` } # NOTE for Perl 5 users: even though the `package` keyword exists, @@ -841,7 +844,7 @@ say "This code took " ~ (time - CHECK time) ~ "s to run"; # ... or clever organization: sub do-db-stuff { - ENTER $db.start-transaction; # create a new transaction everytime we enter the sub + ENTER $db.start-transaction; # New transaction everytime we enter the sub KEEP $db.commit; # commit the transaction if all went well UNDO $db.rollback; # or rollback if all hell broke loose } @@ -951,7 +954,7 @@ say 5!; #=> 120 sub infix:(Int $n, Block $r) { # infix in the middle for ^$n { $r(); # You need the explicit parentheses to call the function in `$r`, - # else you'd be referring at the variable itself, kind of like with `&r`. + # else you'd be referring at the variable itself, like with `&r`. } } 3 times -> { say "hello" }; #=> hello @@ -1004,8 +1007,9 @@ postcircumfix:<{ }>(%h, $key, :delete); # (you can call operators like that) # of the element of the list to be passed to the operator), # or `Any` if there's none (examples below). # -# Otherwise, it pops an element from the list(s) one at a time, and applies the binary function -# to the last result (or the list's first element) and the popped element. +# Otherwise, it pops an element from the list(s) one at a time, and applies +# the binary function to the last result (or the list's first element) +# and the popped element. # # To sum a list, you could use the reduce meta-operator with `+`, i.e.: say [+] 1, 2, 3; #=> 6 @@ -1127,15 +1131,15 @@ for { .say if 'B' ff 'B' for ; #=> B B # because the right-hand-side was tested # directly (and returned `True`). - # "B"s are still printed since it matched that time + # "B"s are printed since it matched that time # (it just went back to `False` right away). .say if 'B' fff 'B' for ; #=> B C B - # because the right-hand-side wasn't tested until + # The right-hand-side wasn't tested until # `$_` became "C" # (and thus did not match instantly). # A flip-flop can change state as many times as needed: -for { +for { .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop", #=> "print this printing again" } @@ -1190,8 +1194,8 @@ say so 'a' ~~ / a /; # More readable with some spaces! # a regexp. We're converting the result using `so`, but in fact, it's # returning a `Match` object. They know how to respond to list indexing, # hash indexing, and return the matched string. -# The results of the match are also available as `$/` (implicitly lexically-scoped). -# You can also use the capture variables (`$0`, `$1`, ... - starting at 0, not 1 !). +# The results of the match are available as `$/` (implicitly lexically-scoped). +# You can also use the capture variables (`$0`, `$1`, ... starting at 0, not 1 !). # # You can also note that `~~` does not perform start/end checking # (meaning the regexp can be matched with just one char of the string), @@ -1233,7 +1237,7 @@ so 'abbbbc' ~~ / a b+ c /; # `True`, matched 4 "b"s so 'ac' ~~ / a b* c /; # `True`, they're all optional. so 'abc' ~~ / a b* c /; # `True` so 'abbbbc' ~~ / a b* c /; # `True` -so 'aec' ~~ / a b* c /; # `False`. "b"(s) are optional, but can't be something else. +so 'aec' ~~ / a b* c /; # `False`. "b"(s) are optional, not replaceable. # - `**` - "Quantify It Yourself". # If you squint hard enough, you might understand @@ -1255,7 +1259,7 @@ so 'fooABCABCbar' ~~ / foo [ A B C ] + bar /; # But this does not go far enough, because we can't actually get back what # we matched. # Capture: We can actually *capture* the results of the regexp, using parentheses. -so 'fooABCABCbar' ~~ / foo ( A B C ) + bar /; # `True`. (we keep `so` here and use `$/` below) +so 'fooABCABCbar' ~~ / foo ( A B C ) + bar /; # `True`. (using `so` here, `$/` below) # So, starting with the grouping explanations. # As we said before, our `Match` object is available as `$/`: @@ -1308,7 +1312,7 @@ sub MAIN($name) { say "Hello, you !" } # And since it's a regular Perl 6 sub, you can haz multi-dispatch: # (using a "Bool" for the named argument so that we get `--replace` # instead of `--replace=1`) -subset File of Str where *.IO.d; # convert to IO object, then check the file exists +subset File of Str where *.IO.d; # convert to IO object to check the file exists multi MAIN('add', $key, $value, Bool :$replace) { ... } multi MAIN('remove', $key) { ... } @@ -1325,7 +1329,9 @@ multi MAIN('import', File, Str :$as) { ... } # omitting parameter name ``` If you want to go further, you can: + - Read the [Perl 6 Advent Calendar](http://perl6advent.wordpress.com/). This is probably the greatest source of Perl 6 information, snippets and such. - Come along on `#perl6` at `irc.freenode.net`. The folks here are always helpful. - Check the [source of Perl 6's functions and classes](https://github.com/rakudo/rakudo/tree/nom/src/core). Rakudo is mainly written in Perl 6 (with a lot of NQP, "Not Quite Perl", a Perl 6 subset easier to implement and optimize). - Read the [Synopses](perlcabal.org/syn). They explain it from an implementor point-of-view, but it's still very interesting. + -- cgit v1.2.3 From 013112b9b338d5f05d33e6d1d85e39e7f061285d Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Wed, 27 Aug 2014 12:38:23 +0200 Subject: Clarifications about *+* and ... with a sub mrf++ --- perl6.html.markdown | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 4ab90914..fe5b197c 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -488,6 +488,7 @@ sub truthy-array(@array) { # (it'll stop at the furthest operator in the current expression) my @arrayplus3 = map(*+3, @array); # `*+3` is the same as `{ $_ + 3 }` my @arrayplus3 = map(*+*+3, @array); # Same as `-> $a, $b { $a + $b + 3 }` + # also `sub ($a, $b) { $a + $b + 3 }` say (*/2)(4); #=> 2 # Immediatly execute the function Whatever created. say ((*+3)/5)(5); #=> 1.6 @@ -496,7 +497,8 @@ say ((*+3)/5)(5); #=> 1.6 # But if you need to have more than one argument (`$_`) # in a block (without wanting to resort to `-> {}`), # you can also use the implicit argument syntax, `$^` : -map({ $^a + $^b + 3 }, @array); # same as the above +map({ $^a + $^b + 3 }, @array); # equivalent to following: +map(sub ($a, $b) { $a + $b + 3 }, @array); # (here with `sub`) # Note : those are sorted lexicographically. # `{ $^b / $^a }` is like `-> $a, $b { $b / $a }` @@ -1072,6 +1074,11 @@ my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalent to the above) my @fib = 1, 1, *+* ... *; # lazy infinite list of prime numbers, # computed using a closure! my @fib = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above) +my @fib = 1, 1, { $^a + $^b } ... *; #(... also equivalent to the above) +# $a and $b will always take the previous values, meaning here +# they'll start with $a = 1 and $b = 1 (values we set by hand). +# then $a = 1 and $b = 2 (result from previous $a+$b), and so on. + say @fib[^10]; #=> 1 1 2 3 5 8 13 21 34 55 # (using a range as the index) # Note : as for ranges, once reified, elements aren't re-calculated. -- cgit v1.2.3 From 931db8c6857ddbf47f4d2a6cb7d905f61cb61e84 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sat, 13 Sep 2014 21:37:35 +0200 Subject: Regexes thingies. --- perl6.html.markdown | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index fe5b197c..4e7d8c6e 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -1246,7 +1246,7 @@ so 'abc' ~~ / a b* c /; # `True` so 'abbbbc' ~~ / a b* c /; # `True` so 'aec' ~~ / a b* c /; # `False`. "b"(s) are optional, not replaceable. -# - `**` - "Quantify It Yourself". +# - `**` - (Unbound) Quantifier # If you squint hard enough, you might understand # why exponentation is used for quantity. so 'abc' ~~ / a b ** 1 c /; # `True` (exactly one time) @@ -1255,6 +1255,27 @@ so 'abbbc' ~~ / a b ** 1..3 c /; # `True` so 'abbbbbbc' ~~ / a b ** 1..3 c /; # `False` (too much) so 'abbbbbbc' ~~ / a b ** 3..* c /; # `True` (infinite ranges are okay) +# - `<[]>` - Character classes +# Character classes are the equivalent of PCRE's `[]` classes, but +# they use a more perl6-ish syntax: +say 'fooa' ~~ / f <[ o a ]>+ /; #=> 'fooa' +# You can use ranges: +say 'aeiou' ~~ / a <[ e..w ]> /; #=> 'aeiou' +# Just like in normal regexes, if you want to use a special character, escape it +# (the last one is escaping a space) +say 'he-he !' ~~ / 'he-' <[ a..z \! \ ]> + /; #=> 'he-he !' +# You'll get a warning if you put duplicate names +# (which has the nice effect of catching the wrote quoting:) +'he he' ~~ / <[ h e ' ' ]> /; # Warns "Repeated characters found in characters class" + +# You can also negate them ... (equivalent to `[^]` in PCRE) +so 'foo' ~~ / <-[ f o ]> + /; # False + +# ... and compose them: : +so 'foo' ~~ / <[ a..z ] - [ f o ]> + /; # False (any letter except f and o) +so 'foo' ~~ / <-[ a..z ] + [ f o ]> + /; # True (no letter except f and o) +so 'foo!' ~~ / <-[ a..z ] + [ f o ]> + /; # True (the + doesn't replace the left part) + ## Grouping and capturing # Group: you can group parts of your regexp with `[]`. # These groups are *not* captured (like PCRE's `(?:)`). @@ -1297,7 +1318,7 @@ say $0.WHAT; #=> (Array) # may it be a range or a specific value (even 1). # If you're wondering how the captures are numbered, here's an explanation: -TODO use graphs from s05 +# (TODO use graphs from s05) ## Alternatives - the `or` of regexps @@ -1305,6 +1326,31 @@ TODO use graphs from s05 so 'abc' ~~ / a [ b | y ] c /; # `True`. Either "b" or "y". so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviously enough ... +# The difference between this `|` and the one you're probably used to is LTM. +# LTM means "Longest Token Matching". This means that the engine will always +# try to match as much as possible in the strng +'foo' ~~ / fo | foo /; # `foo`, because it's longer. +# To decide which part is the "longest", it first splits the regex in two parts: +# The "declarative prefix" (the part that can be statically analyzed) +# and the procedural parts. +# Declarative prefixes include alternations (`|`), conjuctions (`&`), +# sub-rule calls (not yet introduced), literals, characters classes and quantifiers. +# The latter include everything else: back-references, code assertions, +# and other things that can't traditionnaly be represented by normal regexps. +# +# Then, all the alternatives are tried at once, and the longest wins. +# Exemples: +# DECLARATIVE | PROCEDURAL +/ 'foo' \d+ [ || ] /; +# DECLARATIVE (nested groups are not a problem) +/ \s* [ \w & b ] [ c | d ] /; +# However, closures and recursion (of named regexps) are procedural. +# ... There are also more complicated rules, like specificity +# (literals win over character classes) + +# Note: the first-matching `or` still exists, but is now spelled `||` +'foo' ~~ / fo || foo /; # `fo` now. + ### Extra: the MAIN subroutime # The `MAIN` subroutine is called when you run a Perl 6 file directly. # It's very powerful, because Perl 6 actually parses the argument @@ -1317,7 +1363,7 @@ sub MAIN($name) { say "Hello, you !" } # t.pl # And since it's a regular Perl 6 sub, you can haz multi-dispatch: -# (using a "Bool" for the named argument so that we get `--replace` +# (using a "Bool" for the named argument so that we can do `--replace` # instead of `--replace=1`) subset File of Str where *.IO.d; # convert to IO object to check the file exists -- cgit v1.2.3 From b6ba516743d4362b3f079872b1e60c1a54e72aee Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Tue, 23 Sep 2014 23:28:24 +0200 Subject: Exception thingies! --- perl6.html.markdown | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 4e7d8c6e..d12b99ae 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -700,15 +700,37 @@ try { open 'foo'; CATCH { when X::AdHoc { say "unable to open file !" } - # any other exception will be re-raised, since we don't have a `default` + # Any other exception will be re-raised, since we don't have a `default` + # Basically, if a `when` matches (or there's a `default`) marks the exception as + # "handled" so that it doesn't get re-thrown from the `CATCH`. + # You still can re-throw the exception (see below) by hand. } } # You can throw an exception using `die`: die X::AdHoc.new(payload => 'Error !'); -# TODO warn -# TODO fail -# TODO CONTROL + +# You can access the last exception with `$!` (usually used in a `CATCH` block) + +# There are also some subtelties to exceptions. Some Perl 6 subs return a `Failure`, +# which is a kind of "unthrown exception". They're not thrown until you tried to look +# at their content, unless you call `.Bool`/`.defined` on them - then they're handled. +# (the `.handled` method is `rw`, so you can mark it as `False` back yourself) +# +# You can throw a `Failure` using `fail`. Note that if the pragma `use fatal` is on, +# `fail` will throw an exception (like `die`). +fail "foo"; # We're not trying to access the value, so no problem. +try { + fail "foo"; + CATCH { + default { say "It threw because we try to get the fail's value!" } + } +} + +# There is also another kind of exception: Control exceptions. +# Those are "good" exceptions, which happen when you change your program's flow, +# using operators like `return`, `next` or `last`. +# You can "catch" those with `CONTROL` (not 100% working in Rakudo yet). ### Packages # Packages are a way to reuse code. Packages are like "namespaces", and any @@ -1351,6 +1373,9 @@ so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviously enough ... # Note: the first-matching `or` still exists, but is now spelled `||` 'foo' ~~ / fo || foo /; # `fo` now. + + + ### Extra: the MAIN subroutime # The `MAIN` subroutine is called when you run a Perl 6 file directly. # It's very powerful, because Perl 6 actually parses the argument -- cgit v1.2.3 From 160c82684721b62e3369a912542a5f045d286d9c Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Sat, 27 Sep 2014 21:57:10 +0200 Subject: Capturing captures, and numbering them also, multi-indexing in arrays also, labeled loops (draft) also, arrays vs $() vs parcel vs ... (@moritz++) --- perl6.html.markdown | 86 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 10 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index d12b99ae..7afcc930 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -46,18 +46,36 @@ my $inverse = !$bool; # You can invert a bool with the prefix `!` operator my $forced-bool = so $str; # And you can use the prefix `so` operator # which turns its operand into a Bool -## * Arrays. They represent multiple values. Their name start with `@`. +## * Lists. They represent multiple values. Their name start with `@`. -my @array = 1, 2, 3; my @array = 'a', 'b', 'c'; # equivalent to : -my @array = ; # array of words, delimited by space. +my @letters = ; # array of words, delimited by space. # Similar to perl5's qw, or Ruby's %w. +my @array = 1, 2, 3; say @array[2]; # Array indices start at 0 -- This is the third element say "Interpolate an array using [] : @array[]"; -#=> Interpolate an array using [] : a b c +#=> Interpolate an array using [] : 1 2 3 + +@array[0] = -1; # Assign a new value to an array index +@array[0, 1] = 5, 6; # Assign multiple values + +my @keys = 0, 2; +@array[@keys] = @letters; # Assign using an array +say @array; #=> a 2 b + +# There are two more kinds of lists: Parcel and Arrays. +# Parcels are immutable lists (you can't modify a list that's not assigned). +# This is a parcel: +(1, 2, 3); # Not assigned to anything. Changing an element would provoke an error +# This is a list: +my @a = (1, 2, 3); # Assigned to `@a`. Changing elements is okay! + +# Lists flatten (in list context). You'll see below how to apply item context +# or use arrays to have real nested lists. + ## * Hashes. Key-Value Pairs. # Hashes are actually arrays of Pairs (`Key => Value`), @@ -303,6 +321,37 @@ if long-computation() -> $result { say "The result is $result"; } +## Loops can also have a label, and be jumped to through these. +OUTER: while 1 { + say "hey"; + while 1 { + OUTER.last; # All the control keywords must be called on the label itself + } +} + +# Now that you've seen how to traverse a list, you need to be aware of something: +# List context (@) flattens. If you traverse nested lists, you'll actually be traversing a +# shallow list (except if some sub-list were put in item context ($)). +for 1, 2, (3, (4, ((5)))) { + say "Got $_."; +} #=> Got 1. Got 2. Got 3. Got 4. Got 5. + +# ... However: (forcing item context with `$`) +for 1, 2, $(3, 4) { + say "Got $_."; +} #=> Got 1. Got 2. Got 3 4. + +# Note that the last one actually joined 3 and 4. +# While `$(...)` will apply item to context to just about anything, you can also create +# an array using `[]`: +for [1, 2, 3, 4] { + say "Got $_."; +} #=> Got 1 2 3 4. + +# The other difference between `$()` and `[]` is that `[]` always returns a mutable Array +# whereas `$()` will return a Parcel when given a Parcel. + + ### Operators ## Since Perl languages are very much operator-based languages @@ -359,9 +408,9 @@ $arg ~~ &bool-returning-function; # `True` if the function, passed `$arg` # This also works as a shortcut for `0..^N`: ^10; # means 0..^10 -# This also allows us to demonstrate that Perl 6 has lazy arrays, +# This also allows us to demonstrate that Perl 6 has lazy/infinite arrays, # using the Whatever Star: -my @array = 1..*; # 1 to Infinite ! +my @array = 1..*; # 1 to Infinite ! `1..Inf` is the same. say @array[^10]; # you can pass arrays as subscripts and it'll return # an array of results. This will print # "1 2 3 4 5 6 7 8 9 10" (and not run out of memory !) @@ -372,6 +421,13 @@ say @array[^10]; # you can pass arrays as subscripts and it'll return # Perl 6 will be forced to try and evaluate the whole array (to print it), # so you'll end with an infinite loop. +# You can use that in most places you'd expect, even assigning to an array +my @numbers = ^20; +@numbers[5..*] = 3, 9 ... * > 90; # The right hand side could be infinite as well. + # (but not both, as this would be an infinite loop) +say @numbers; #=> 3 9 15 21 27 [...] 81 87 + + ## * And, Or 3 && 4; # 4, which is Truthy. Calls `.Bool` on `4` and gets `True`. 0 || False; # False. Calls `.Bool` on `0` @@ -1325,7 +1381,7 @@ say $0; # The same as above. # You might be wondering why it's an array, and the answer is simple: # Some capture (indexed using `$0`, `$/[0]` or a named one) will be an array # IFF it can have more than one element -# (so, with `*`, `+` and any `**`, but not with `?`). +# (so, with `*`, `+` and `**` (whatever the operands), but not with `?`). # Let's use examples to see that: so 'fooABCbar' ~~ / foo ( A B C )? bar /; # `True` say $/[0]; #=> 「ABC」 @@ -1339,16 +1395,26 @@ say $0.WHAT; #=> (Array) # A specific quantifier will always capture an Array, # may it be a range or a specific value (even 1). -# If you're wondering how the captures are numbered, here's an explanation: -# (TODO use graphs from s05) +# The captures are indexed per nesting. This means a group in a group will be nested +# under its parent group: `$/[0][0]`, for this code: +'hello-~-world' ~~ / ( 'hello' ( <[ \- \~ ]> + ) ) 'world' /; +say $/[0].Str; #=> hello~ +say $/[0][0].Str; #=> ~ +# This stems from a very simple fact: `$/` does not contain strings, integers or arrays, +# it only contains match objects. These contain the `.list`, `.hash` and `.Str` methods. +# (but you can also just use `match` for hash access and `match[idx]` for array access) +say $/[0].list.perl; #=> (Match.new(...),).list + # We can see it's a list of Match objects. Those contain a bunch of infos: + # where the match started/ended, the "ast" (see actions later), etc. + # You'll see named capture below with grammars. ## Alternatives - the `or` of regexps # WARNING: They are DIFFERENT from PCRE regexps. so 'abc' ~~ / a [ b | y ] c /; # `True`. Either "b" or "y". so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviously enough ... -# The difference between this `|` and the one you're probably used to is LTM. +# The difference between this `|` and the one you're used to is LTM. # LTM means "Longest Token Matching". This means that the engine will always # try to match as much as possible in the strng 'foo' ~~ / fo | foo /; # `foo`, because it's longer. -- cgit v1.2.3 From d786c94f8408b2fb2f0a0b39c7c5cc8ec77e0808 Mon Sep 17 00:00:00 2001 From: Nami-Doc Date: Tue, 7 Oct 2014 15:47:06 +0200 Subject: Fix a braino caught by @wryk++ --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 7afcc930..ee27ff42 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -1447,7 +1447,7 @@ so 'ayc' ~~ / a [ b | y ] c /; # `True`. Obviously enough ... # It's very powerful, because Perl 6 actually parses the argument # and pass them as such to the sub. It also handles named argument (`--foo`) # and will even go as far as to autogenerate a `--help` -sub MAIN($name) { say "Hello, you !" } +sub MAIN($name) { say "Hello, $name !" } # This produces: # $ perl6 cli.pl # Usage: -- cgit v1.2.3 From 74533ea29279c82957b45c6af0083e344800a17c Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Sun, 12 Oct 2014 19:41:07 +0200 Subject: Fix a comment that indicates what "say @array" will print. We assigned 6 to @array[1] a few lines before, so say @array will print "a 6 b", not "a 2 b". --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index ee27ff42..52625bc2 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -64,7 +64,7 @@ say "Interpolate an array using [] : @array[]"; my @keys = 0, 2; @array[@keys] = @letters; # Assign using an array -say @array; #=> a 2 b +say @array; #=> a 6 b # There are two more kinds of lists: Parcel and Arrays. # Parcels are immutable lists (you can't modify a list that's not assigned). -- cgit v1.2.3 From 9576d80b75a025740775b7a16ee327403df25e88 Mon Sep 17 00:00:00 2001 From: Kishore Relangi Date: Mon, 10 Nov 2014 15:50:47 +0530 Subject: Update perl6.html.markdown It is actually generating fibonacci series --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 52625bc2..9f3a03ba 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -1149,7 +1149,7 @@ my @list = 1, 3, 9 ... * > 30; # you can use a predicate # (with the Whatever Star, here). my @list = 1, 3, 9 ... { $_ > 30 }; # (equivalent to the above) -my @fib = 1, 1, *+* ... *; # lazy infinite list of prime numbers, +my @fib = 1, 1, *+* ... *; # lazy infinite list of fibonacci series, # computed using a closure! my @fib = 1, 1, -> $a, $b { $a + $b } ... *; # (equivalent to the above) my @fib = 1, 1, { $^a + $^b } ... *; #(... also equivalent to the above) -- cgit v1.2.3 From acbea221a4037ff34ebf8a9b76b47aaec3c0c755 Mon Sep 17 00:00:00 2001 From: ugexe Date: Sun, 16 Nov 2014 12:16:26 -0500 Subject: 0 // 5 = 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 11:59 < nebuchadnezzar> m: say Any // Nil // 0 // 5 11:59 <+camelia> rakudo-moar 38e77b: OUTPUT«0␤» 11:59 < nebuchadnezzar> shouldn't it be 5? 12:00 < ugexe> 0 is a defined value 12:00 < ugexe> m: say Any || Nil || 0 || 5; 12:00 <+camelia> rakudo-moar 38e77b: OUTPUT«5␤» --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 9f3a03ba..b178de1e 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -1177,7 +1177,7 @@ $obj eqv $obj2; # sort comparison using eqv semantics ## * Short-circuit default operator # Like `or` and `||`, but instead returns the first *defined* value : -say Any // Nil // 0 // 5; #=> 5 +say Any // Nil // 0 // 5; #=> 0 ## * Short-circuit exclusive or (XOR) # Returns `True` if one (and only one) of its arguments is true -- cgit v1.2.3 From f323083ba49a2e1fa0b5a11b4ce2bf74a21cac86 Mon Sep 17 00:00:00 2001 From: ven Date: Wed, 24 Dec 2014 18:22:48 +0100 Subject: rework of the tutorial --- perl6.html.markdown | 356 ++++++++++++++++++++++++++-------------------------- 1 file changed, 180 insertions(+), 176 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index b178de1e..1536b152 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -35,7 +35,8 @@ my $variable; ## * Scalars. They represent a single value. They start with a `$` my $str = 'String'; -my $str2 = "String"; # double quotes allow for interpolation +# double quotes allow for interpolation (which we'll see later): +my $str2 = "String"; # variable names can contain but not end with simple quotes and dashes, # and can contain (and end with) underscores : @@ -66,23 +67,13 @@ my @keys = 0, 2; @array[@keys] = @letters; # Assign using an array say @array; #=> a 6 b -# There are two more kinds of lists: Parcel and Arrays. -# Parcels are immutable lists (you can't modify a list that's not assigned). -# This is a parcel: -(1, 2, 3); # Not assigned to anything. Changing an element would provoke an error -# This is a list: -my @a = (1, 2, 3); # Assigned to `@a`. Changing elements is okay! - -# Lists flatten (in list context). You'll see below how to apply item context -# or use arrays to have real nested lists. - - -## * Hashes. Key-Value Pairs. -# Hashes are actually arrays of Pairs (`Key => Value`), -# except they get "flattened", removing duplicated keys. +## * Hashes, or key-value Pairs. +# Hashes are actually arrays of Pairs +# (you can construct a Pair object using the syntax `Key => Value`), +# except they get "flattened" (hash context), removing duplicated keys. my %hash = 1 => 2, 3 => 4; -my %hash = autoquoted => "key", # keys *can* get auto-quoted +my %hash = autoquoted => "key", # keys get auto-quoted "some other" => "value", # trailing commas are okay ; my %hash = ; # you can also create a hash @@ -112,30 +103,6 @@ sub say-hello-to(Str $name) { # You can provide the type of an argument say "Hello, $name !"; } -# Since you can omit parenthesis to call a function with no arguments, -# you need "&" in the name to capture `say-hello`. -my &s = &say-hello; -my &other-s = sub { say "Anonymous function !" } - -# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" -sub as-many($head, *@rest) { # `*@` (slurpy) will basically "take everything else". - # Note: you can have parameters *before* (like here) - # a slurpy one, but not *after*. - say @rest.join(' / ') ~ " !"; -} -say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday ! - # Note that the splat did not consume - # the parameter before. - -## You can call a function with an array using the -# "argument list flattening" operator `|` -# (it's not actually the only role of this operator, but it's one of them) -sub concat3($a, $b, $c) { - say "$a, $b, $c"; -} -concat3(|@array); #=> a, b, c - # `@array` got "flattened" as a part of the argument list - ## It can also have optional arguments: sub with-optional($arg?) { # the "?" marks the argument optional say "I might return `(Any)` if I don't have an argument passed, @@ -154,23 +121,20 @@ hello-to; #=> Hello, World ! hello-to(); #=> Hello, World ! hello-to('You'); #=> Hello, You ! -## You can also, by using a syntax akin to the one of hashes (yay unification !), +## You can also, by using a syntax akin to the one of hashes (yay unified syntax !), ## pass *named* arguments to a `sub`. +# They're optional, and will default to "Any" (Perl's "null"-like value). sub with-named($normal-arg, :$named) { say $normal-arg + $named; } with-named(1, named => 6); #=> 7 # There's one gotcha to be aware of, here: # If you quote your key, Perl 6 won't be able to see it at compile time, -# and you'll have a single Pair object as a positional paramater. +# and you'll have a single Pair object as a positional paramater, +# which means this fails: +with-named(1, 'named' => 6); with-named(2, :named(5)); #=> 7 -with-named(3, :4named); #=> 7 - # (special colon pair syntax for numbers, - # to be used with s// and such, see later) - -with-named(3); # warns, because we tried to use the undefined $named in a `+`: - # by default, named arguments are *optional* # To make a named argument mandatory, you can use `?`'s inverse, `!` sub with-mandatory-named(:$str!) { @@ -187,11 +151,6 @@ sub takes-a-bool($name, :$bool) { # ... you can use the same "short boolean" hash syntax: takes-a-bool('config', :bool); # config takes True takes-a-bool('config', :!bool); # config takes False -# or you can use the "adverb" form: -takes-a-bool('config'):bool; #=> config takes True -takes-a-bool('config'):!bool; #=> config takes False -# You'll learn to love (or maybe hate, eh) that syntax later. - ## You can also provide your named arguments with defaults: sub named-def(:$def = 5) { @@ -201,8 +160,29 @@ named-def; #=> 5 named-def(:10def); #=> 10 named-def(def => 15); #=> 15 -# -- Note: we're going to learn *more* on subs really soon, -# but we need to grasp a few more things to understand their real power. Ready? +# Since you can omit parenthesis to call a function with no arguments, +# you need "&" in the name to capture `say-hello`. +my &s = &say-hello; +my &other-s = sub { say "Anonymous function !" } + +# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many" +sub as-many($head, *@rest) { # `*@` (slurpy) will basically "take everything else". + # Note: you can have parameters *before* (like here) + # a slurpy one, but not *after*. + say @rest.join(' / ') ~ " !"; +} +say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday ! + # Note that the splat did not consume + # the parameter before. + +## You can call a function with an array using the +# "argument list flattening" operator `|` +# (it's not actually the only role of this operator, but it's one of them) +sub concat3($a, $b, $c) { + say "$a, $b, $c"; +} +concat3(|@array); #=> a, b, c + # `@array` got "flattened" as a part of the argument list ### Containers # In Perl 6, values are actually stored in "containers". @@ -220,17 +200,13 @@ sub mutate($n is rw) { # A sub itself returns a container, which means it can be marked as rw: my $x = 42; -sub mod() is rw { $x } -mod() = 52; # in this case, the parentheses are mandatory - # (else Perl 6 thinks `mod` is a "term") +sub x-store() is rw { $x } +x-store() = 52; # in this case, the parentheses are mandatory + # (else Perl 6 thinks `mod` is an identifier) say $x; #=> 52 ### Control Flow Structures - -# You don't need to put parenthesis around the condition, -# but that also means you always have to use brackets (`{ }`) for their body: - ## Conditionals # - `if` @@ -247,30 +223,38 @@ unless False { say "It's not false !"; } +# As you can see, you don't need parentheses around conditions. +# However, you do need the brackets around the "body" block: +# if (true) say; # This doesn't work ! + # You can also use their postfix versions, with the keyword after: say "Quite truthy" if True; -# if (true) say; # This doesn't work ! - # - Ternary conditional, "?? !!" (like `x ? y : z` in some other languages) my $a = $condition ?? $value-if-true !! $value-if-false; # - `given`-`when` looks like other languages `switch`, but much more # powerful thanks to smart matching and thanks to Perl 6's "topic variable", $_. +# # This variable contains the default argument of a block, # a loop's current iteration (unless explicitly named), etc. +# # `given` simply puts its argument into `$_` (like a block would do), # and `when` compares it using the "smart matching" (`~~`) operator. +# # Since other Perl 6 constructs use this variable (as said before, like `for`, # blocks, etc), this means the powerful `when` is not only applicable along with # a `given`, but instead anywhere a `$_` exists. given "foo bar" { - when /foo/ { # Don't worry about smart matching -- just know `when` uses it. + say $_; #=> foo bar + when /foo/ { # Don't worry about smart matching yet – just know `when` uses it. # This is equivalent to `if $_ ~~ /foo/`. say "Yay !"; } when $_.chars > 50 { # smart matching anything with True (`$a ~~ True`) is True, # so you can also put "normal" conditionals. + # This when is equivalent to this `if`: + # if ($_.chars > 50) ~~ True {...} say "Quite a long string !"; } default { # same as `when *` (using the Whatever Star) @@ -281,7 +265,7 @@ given "foo bar" { ## Looping constructs # - `loop` is an infinite loop if you don't pass it arguments, -# but can also be a c-style `for`: +# but can also be a C-style `for` loop: loop { say "This is an infinite loop !"; last; # last breaks out of the loop, like the `break` keyword in other languages @@ -296,7 +280,7 @@ loop (my $i = 0; $i < 5; $i++) { # - `for` - Passes through an array for @array -> $variable { - say "I've found $variable !"; + say "I've got $variable !"; } # As we saw with given, for's default "current iteration" variable is `$_`. @@ -316,22 +300,15 @@ for @array { last if $_ == 5; # Or break out of a loop (like `break` in C-like languages). } -# Note - the "lambda" `->` syntax isn't reserved to `for`: +# The "pointy block" syntax isn't specific to for. +# It's just a way to express a block in Perl6. if long-computation() -> $result { say "The result is $result"; } -## Loops can also have a label, and be jumped to through these. -OUTER: while 1 { - say "hey"; - while 1 { - OUTER.last; # All the control keywords must be called on the label itself - } -} - # Now that you've seen how to traverse a list, you need to be aware of something: # List context (@) flattens. If you traverse nested lists, you'll actually be traversing a -# shallow list (except if some sub-list were put in item context ($)). +# shallow list. for 1, 2, (3, (4, ((5)))) { say "Got $_."; } #=> Got 1. Got 2. Got 3. Got 4. Got 5. @@ -348,9 +325,14 @@ for [1, 2, 3, 4] { say "Got $_."; } #=> Got 1 2 3 4. -# The other difference between `$()` and `[]` is that `[]` always returns a mutable Array -# whereas `$()` will return a Parcel when given a Parcel. +# You need to be aware of when flattening happens exactly. +# The general guideline is that argument lists flatten, but not method calls. +# Also note that `.list` and array assignment flatten (`@ary = ...`) flatten. +((1,2), 3, (4,5)).map({...}); # iterates over three elements (method call) +map {...}, ((1,2),3,(4,5)); # iterates over five elements (argument list is flattened) +(@a, @b, @c).pick(1); # picks one of three arrays (method call) +pick 1, @a, @b, @c; # flattens argument list and pick one element ### Operators @@ -394,9 +376,6 @@ $arg ~~ &bool-returning-function; # `True` if the function, passed `$arg` 1 ~~ True; # smart-matching against a boolean always returns that boolean # (and will warn). -# - `===` is value identity and uses `.WHICH` on the objects to compare them -# - `=:=` is container identity and uses `VAR()` on the objects to compare them - # You also, of course, have `<`, `<=`, `>`, `>=`. # Their string equivalent are also avaiable : `lt`, `le`, `gt`, `ge`. 3 > 4; @@ -559,6 +538,21 @@ map(sub ($a, $b) { $a + $b + 3 }, @array); # (here with `sub`) # Note : those are sorted lexicographically. # `{ $^b / $^a }` is like `-> $a, $b { $b / $a }` +## About types... +# Perl6 is gradually typed. This means you can specify the type +# of your variables/arguments/return types, or you can omit them +# and they'll default to "Any". +# You obviously get access to a few base types, like Int and Str. +# The constructs for declaring types are "class", "role", +# which you'll see later. + +# For now, let us examinate "subset": +# a "subset" is a "sub-type" with additional checks. +# For example: "a very big integer is an Int that's greater than 500" +# You can specify the type you're subtyping (by default, Any), +# and add additional checks with the "where" keyword: +subset VeryBigInteger of Int where * > 500; + ## Multiple Dispatch # Perl 6 can decide which variant of a `sub` to call based on the type of the # arguments, or on arbitrary preconditions, like with a type or a `where`: @@ -567,20 +561,19 @@ map(sub ($a, $b) { $a + $b + 3 }, @array); # (here with `sub`) multi sub sayit(Int $n) { # note the `multi` keyword here say "Number: $n"; } -multi sayit(Str $s) } # the `sub` is the default +multi sayit(Str $s) } # a multi is a `sub` by default say "String: $s"; } sayit("foo"); # prints "String: foo" sayit(True); # fails at *compile time* with # "calling 'sayit' will never work with arguments of types ..." -# with arbitrary precondition: +# with arbitrary precondition (remember subsets?): multi is-big(Int $n where * > 50) { "Yes !" } # using a closure multi is-big(Int $ where 10..50) { "Quite." } # Using smart-matching # (could use a regexp, etc) multi is-big(Int $) { "No" } -# You can also name these checks, by creating "subsets": subset Even of Int where * %% 2; multi odd-or-even(Even) { "Even" } # The main case using the type. @@ -724,7 +717,7 @@ role PrintableVal { } } -# you "use" a mixin with "does" : +# you "import" a mixin (a "role") with "does": class Item does PrintableVal { has $.val; @@ -1083,9 +1076,7 @@ postcircumfix:<{ }>(%h, $key, :delete); # (you can call operators like that) # It's a prefix meta-operator that takes a binary functions and # one or many lists. If it doesn't get passed any argument, # it either return a "default value" for this operator -# (a value that wouldn't change the result if passed as one -# of the element of the list to be passed to the operator), -# or `Any` if there's none (examples below). +# (a meaningless value) or `Any` if there's none (examples below). # # Otherwise, it pops an element from the list(s) one at a time, and applies # the binary function to the last result (or the list's first element) @@ -1107,9 +1098,7 @@ say [//] Nil, Any, False, 1, 5; #=> False # Default value examples: say [*] (); #=> 1 say [+] (); #=> 0 - # In both cases, they're results that, were they in the lists, - # wouldn't have any impact on the final value - # (since N*1=N and N+0=N). + # meaningless values, since N*1=N and N+0=N. say [//]; #=> (Any) # There's no "default value" for `//`. @@ -1163,90 +1152,6 @@ say @fib[^10]; #=> 1 1 2 3 5 8 13 21 34 55 # That's why `@primes[^100]` will take a long time the first time you print # it, then be instant. - -## * Sort comparison -# They return one value of the `Order` enum : `Less`, `Same` and `More` -# (which numerify to -1, 0 or +1). -1 <=> 4; # sort comparison for numerics -'a' leg 'b'; # sort comparison for string -$obj eqv $obj2; # sort comparison using eqv semantics - -## * Generic ordering -3 before 4; # True -'b' after 'a'; # True - -## * Short-circuit default operator -# Like `or` and `||`, but instead returns the first *defined* value : -say Any // Nil // 0 // 5; #=> 0 - -## * Short-circuit exclusive or (XOR) -# Returns `True` if one (and only one) of its arguments is true -say True ^^ False; #=> True - -## * Flip Flop -# The flip flop operators (`ff` and `fff`, equivalent to P5's `..`/`...`). -# are operators that take two predicates to test: -# They are `False` until their left side returns `True`, then are `True` until -# their right side returns `True`. -# Like for ranges, you can exclude the iteration when it became `True`/`False` -# by using `^` on either side. -# Let's start with an example : -for { - # by default, `ff`/`fff` smart-match (`~~`) against `$_`: - if 'met' ^ff 'meet' { # Won't enter the if for "met" - # (explained in details below). - .say - } - - if rand == 0 ff rand == 1 { # compare variables other than `$_` - say "This ... probably will never run ..."; - } -} -# This will print "young hero we shall meet" (excluding "met"): -# the flip-flop will start returning `True` when it first encounters "met" -# (but will still return `False` for "met" itself, due to the leading `^` -# on `ff`), until it sees "meet", which is when it'll start returning `False`. - -# The difference between `ff` (awk-style) and `fff` (sed-style) is that -# `ff` will test its right side right when its left side changes to `True`, -# and can get back to `False` right away -# (*except* it'll be `True` for the iteration that matched) - -# While `fff` will wait for the next iteration to -# try its right side, once its left side changed: -.say if 'B' ff 'B' for ; #=> B B - # because the right-hand-side was tested - # directly (and returned `True`). - # "B"s are printed since it matched that time - # (it just went back to `False` right away). -.say if 'B' fff 'B' for ; #=> B C B - # The right-hand-side wasn't tested until - # `$_` became "C" - # (and thus did not match instantly). - -# A flip-flop can change state as many times as needed: -for { - .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop", - #=> "print this printing again" -} - -# you might also use a Whatever Star, -# which is equivalent to `True` for the left side or `False` for the right: -for (1, 3, 60, 3, 40, 60) { # Note: the parenthesis are superfluous here - # (sometimes called "superstitious parentheses") - .say if $_ > 50 ff *; # Once the flip-flop reaches a number greater than 50, - # it'll never go back to `False` - #=> 60 3 40 60 -} - -# You can also use this property to create an `If` -# that'll not go through the first time : -for { - .say if * ^ff *; # the flip-flop is `True` and never goes back to `False`, - # but the `^` makes it *not run* on the first iteration - #=> b c -} - - ### Regular Expressions # I'm sure a lot of you have been waiting for this one. # Well, now that you know a good deal of Perl 6 already, we can get started. @@ -1470,6 +1375,105 @@ multi MAIN('import', File, Str :$as) { ... } # omitting parameter name # As you can see, this is *very* powerful. # It even went as far as to show inline the constants. # (the type is only displayed if the argument is `$`/is named) + +### +### APPENDIX A: +### +### List of things +### + +# It's considered by now you know the Perl6 basics. +# This section is just here to list some common operations, +# but which are not in the "main part" of the tutorial to bloat it up + +## Operators + + +## * Sort comparison +# They return one value of the `Order` enum : `Less`, `Same` and `More` +# (which numerify to -1, 0 or +1). +1 <=> 4; # sort comparison for numerics +'a' leg 'b'; # sort comparison for string +$obj eqv $obj2; # sort comparison using eqv semantics + +## * Generic ordering +3 before 4; # True +'b' after 'a'; # True + +## * Short-circuit default operator +# Like `or` and `||`, but instead returns the first *defined* value : +say Any // Nil // 0 // 5; #=> 0 + +## * Short-circuit exclusive or (XOR) +# Returns `True` if one (and only one) of its arguments is true +say True ^^ False; #=> True +## * Flip Flop +# The flip flop operators (`ff` and `fff`, equivalent to P5's `..`/`...`). +# are operators that take two predicates to test: +# They are `False` until their left side returns `True`, then are `True` until +# their right side returns `True`. +# Like for ranges, you can exclude the iteration when it became `True`/`False` +# by using `^` on either side. +# Let's start with an example : +for { + # by default, `ff`/`fff` smart-match (`~~`) against `$_`: + if 'met' ^ff 'meet' { # Won't enter the if for "met" + # (explained in details below). + .say + } + + if rand == 0 ff rand == 1 { # compare variables other than `$_` + say "This ... probably will never run ..."; + } +} +# This will print "young hero we shall meet" (excluding "met"): +# the flip-flop will start returning `True` when it first encounters "met" +# (but will still return `False` for "met" itself, due to the leading `^` +# on `ff`), until it sees "meet", which is when it'll start returning `False`. + +# The difference between `ff` (awk-style) and `fff` (sed-style) is that +# `ff` will test its right side right when its left side changes to `True`, +# and can get back to `False` right away +# (*except* it'll be `True` for the iteration that matched) - +# While `fff` will wait for the next iteration to +# try its right side, once its left side changed: +.say if 'B' ff 'B' for ; #=> B B + # because the right-hand-side was tested + # directly (and returned `True`). + # "B"s are printed since it matched that time + # (it just went back to `False` right away). +.say if 'B' fff 'B' for ; #=> B C B + # The right-hand-side wasn't tested until + # `$_` became "C" + # (and thus did not match instantly). + +# A flip-flop can change state as many times as needed: +for { + .say if $_ eq 'start' ^ff^ $_ eq 'stop'; # exclude both "start" and "stop", + #=> "print this printing again" +} + +# you might also use a Whatever Star, +# which is equivalent to `True` for the left side or `False` for the right: +for (1, 3, 60, 3, 40, 60) { # Note: the parenthesis are superfluous here + # (sometimes called "superstitious parentheses") + .say if $_ > 50 ff *; # Once the flip-flop reaches a number greater than 50, + # it'll never go back to `False` + #=> 60 3 40 60 +} + +# You can also use this property to create an `If` +# that'll not go through the first time : +for { + .say if * ^ff *; # the flip-flop is `True` and never goes back to `False`, + # but the `^` makes it *not run* on the first iteration + #=> b c +} + + +# - `===` is value identity and uses `.WHICH` on the objects to compare them +# - `=:=` is container identity and uses `VAR()` on the objects to compare them + ``` If you want to go further, you can: -- cgit v1.2.3 From 1c52c5ea125285eb8a3af5cf4cb510e9c3e7a49b Mon Sep 17 00:00:00 2001 From: ven Date: Mon, 29 Dec 2014 22:48:01 +0100 Subject: @wryk++ --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 1536b152..b10e04bf 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -212,7 +212,7 @@ say $x; #=> 52 # - `if` # Before talking about `if`, we need to know which values are "Truthy" # (represent True), and which are "Falsey" (or "Falsy") -- represent False. -# Only these values are Falsey: (), 0, "0", Nil, A type (like `Str` or `Int`), +# Only these values are Falsey: (), 0, "0", "", Nil, A type (like `Str` or `Int`), # and of course False itself. # Every other value is Truthy. if True { -- cgit v1.2.3 From 4ef0e78009896395804c609201c9b70254bddcb2 Mon Sep 17 00:00:00 2001 From: raiph Date: Fri, 9 Jan 2015 18:39:56 -0500 Subject: Fix link to synopses and call them 'design docs' --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index b10e04bf..13f383fe 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -1481,5 +1481,5 @@ If you want to go further, you can: - Read the [Perl 6 Advent Calendar](http://perl6advent.wordpress.com/). This is probably the greatest source of Perl 6 information, snippets and such. - Come along on `#perl6` at `irc.freenode.net`. The folks here are always helpful. - Check the [source of Perl 6's functions and classes](https://github.com/rakudo/rakudo/tree/nom/src/core). Rakudo is mainly written in Perl 6 (with a lot of NQP, "Not Quite Perl", a Perl 6 subset easier to implement and optimize). - - Read the [Synopses](perlcabal.org/syn). They explain it from an implementor point-of-view, but it's still very interesting. + - Read [the language design documents](http://design.perl6.org). They explain P6 from an implementor point-of-view, but it's still very interesting. -- cgit v1.2.3 From 63749de9722cca6c96fe186d43afeebc86790c09 Mon Sep 17 00:00:00 2001 From: ven Date: Sun, 25 Jan 2015 21:24:50 +0100 Subject: fix #937 --- perl6.html.markdown | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 13f383fe..72faecb6 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -157,7 +157,6 @@ sub named-def(:$def = 5) { say $def; } named-def; #=> 5 -named-def(:10def); #=> 10 named-def(def => 15); #=> 15 # Since you can omit parenthesis to call a function with no arguments, @@ -653,7 +652,7 @@ class A { has Int $!private-field = 10; method get-value { - $.field + $!private-field + $n; + $.field + $!private-field; } method set-value($n) { @@ -671,7 +670,7 @@ class A { # Create a new instance of A with $.field set to 5 : # Note: you can't set private-field from here (more later on). my $a = A.new(field => 5); -$a.get-value; #=> 18 +$a.get-value; #=> 15 #$a.field = 5; # This fails, because the `has $.field` is immutable $a.other-field = 10; # This, however, works, because the public field # is mutable (`rw`). -- cgit v1.2.3 From 862d90da8f753222a518d5ffb4631b46f9dffc91 Mon Sep 17 00:00:00 2001 From: Philippe Bricout Date: Fri, 27 Feb 2015 16:31:15 +0100 Subject: Update perl6.html.markdown typo, wrong sub name in comment : mod => x-store --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 72faecb6..3f1cab2e 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -201,7 +201,7 @@ sub mutate($n is rw) { my $x = 42; sub x-store() is rw { $x } x-store() = 52; # in this case, the parentheses are mandatory - # (else Perl 6 thinks `mod` is an identifier) + # (else Perl 6 thinks `x-store` is an identifier) say $x; #=> 52 -- cgit v1.2.3 From cd462d1cc53183ca9fcc2567308e5ce24f162f89 Mon Sep 17 00:00:00 2001 From: Philippe Bricout Date: Sun, 1 Mar 2015 18:04:05 +0100 Subject: Perl6, minor change in comments It seems "when" should be replace by "given" at the end of the comment. --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 3f1cab2e..85ab1d79 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -283,7 +283,7 @@ for @array -> $variable { } # As we saw with given, for's default "current iteration" variable is `$_`. -# That means you can use `when` in a `for` just like you were in a when. +# That means you can use `when` in a `for` just like you were in a `given`. for @array { say "I've got $_"; -- cgit v1.2.3 From 7243f13fc6462e9fee8d463e13446ab7339e9d67 Mon Sep 17 00:00:00 2001 From: ronaldxs Date: Fri, 20 Mar 2015 16:40:25 -0400 Subject: thrice .... gather ^3 counts three times "0 1 2" not 5 Probably just a paste-o mistake. Want to count 3 times not 5. --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 85ab1d79..1b320028 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -963,7 +963,7 @@ say join ',', gather if False { # But consider: constant thrice = gather for ^3 { say take $_ }; # Doesn't print anything # versus: -constant thrice = eager gather for ^3 { say take $_ }; #=> 0 1 2 3 4 +constant thrice = eager gather for ^3 { say take $_ }; #=> 0 1 2 # - `lazy` - Defer actual evaluation until value is fetched (forces lazy context) # Not yet implemented !! -- cgit v1.2.3 From 965d7972d1ef7dfbfc9c07de72bbce81898eb703 Mon Sep 17 00:00:00 2001 From: Jeff Erickson Date: Wed, 25 Mar 2015 16:53:46 -0400 Subject: Minor typo: fixed curly bracket direction (} -> {) --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 1b320028..f0ef6600 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -560,7 +560,7 @@ subset VeryBigInteger of Int where * > 500; multi sub sayit(Int $n) { # note the `multi` keyword here say "Number: $n"; } -multi sayit(Str $s) } # a multi is a `sub` by default +multi sayit(Str $s) { # a multi is a `sub` by default say "String: $s"; } sayit("foo"); # prints "String: foo" -- cgit v1.2.3 From 4c46a456bd5e33aec2a3cab7b7c33a375d5a97ed Mon Sep 17 00:00:00 2001 From: Philippe Bricout Date: Wed, 1 Apr 2015 15:13:59 +0200 Subject: Update perl6.html.markdown ($_.chars > 50) ~~ True : this is always True. --- perl6.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index f0ef6600..8404f9f8 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -253,7 +253,9 @@ given "foo bar" { when $_.chars > 50 { # smart matching anything with True (`$a ~~ True`) is True, # so you can also put "normal" conditionals. # This when is equivalent to this `if`: - # if ($_.chars > 50) ~~ True {...} + # if $_ ~~ ($_.chars > 50) {...} + # Which means: + # if $_.chars > 50 {...} say "Quite a long string !"; } default { # same as `when *` (using the Whatever Star) -- cgit v1.2.3 From 6e9ccfaea3bf2963dc624cd7a6cc198cd1147aa9 Mon Sep 17 00:00:00 2001 From: ven Date: Wed, 15 Apr 2015 14:33:52 +0200 Subject: fix #1032 and a few more fixes/moving arounds --- perl6.html.markdown | 60 ++++++++++++++++------------------------------------- 1 file changed, 18 insertions(+), 42 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 8404f9f8..b2d7d48c 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -307,37 +307,9 @@ if long-computation() -> $result { say "The result is $result"; } -# Now that you've seen how to traverse a list, you need to be aware of something: -# List context (@) flattens. If you traverse nested lists, you'll actually be traversing a -# shallow list. -for 1, 2, (3, (4, ((5)))) { - say "Got $_."; -} #=> Got 1. Got 2. Got 3. Got 4. Got 5. - -# ... However: (forcing item context with `$`) -for 1, 2, $(3, 4) { - say "Got $_."; -} #=> Got 1. Got 2. Got 3 4. - -# Note that the last one actually joined 3 and 4. -# While `$(...)` will apply item to context to just about anything, you can also create -# an array using `[]`: -for [1, 2, 3, 4] { - say "Got $_."; -} #=> Got 1 2 3 4. - -# You need to be aware of when flattening happens exactly. -# The general guideline is that argument lists flatten, but not method calls. -# Also note that `.list` and array assignment flatten (`@ary = ...`) flatten. -((1,2), 3, (4,5)).map({...}); # iterates over three elements (method call) -map {...}, ((1,2),3,(4,5)); # iterates over five elements (argument list is flattened) - -(@a, @b, @c).pick(1); # picks one of three arrays (method call) -pick 1, @a, @b, @c; # flattens argument list and pick one element - ### Operators -## Since Perl languages are very much operator-based languages +## Since Perl languages are very much operator-based languages, ## Perl 6 operators are actually just funny-looking subroutines, in syntactic ## categories, like infix:<+> (addition) or prefix: (bool not). @@ -396,17 +368,21 @@ say @array[^10]; # you can pass arrays as subscripts and it'll return # "1 2 3 4 5 6 7 8 9 10" (and not run out of memory !) # Note : when reading an infinite list, Perl 6 will "reify" the elements # it needs, then keep them in memory. They won't be calculated more than once. - -# Warning, though: if you try this example in the REPL and just put `1..*`, -# Perl 6 will be forced to try and evaluate the whole array (to print it), -# so you'll end with an infinite loop. - -# You can use that in most places you'd expect, even assigning to an array -my @numbers = ^20; -@numbers[5..*] = 3, 9 ... * > 90; # The right hand side could be infinite as well. - # (but not both, as this would be an infinite loop) -say @numbers; #=> 3 9 15 21 27 [...] 81 87 - +# It also will never calculate more elements that are needed. + +# An array subscript can also be a closure. +# It'll be called with the length as the argument +say join(' ', @array[15..*]); #=> 15 16 17 18 19 +# which is equivalent to: +say join(' ', @array[-> $n { 15..$n }]); + +# You can use that in most places you'd expect, even assigning to an array +my @numbers = ^20; +my @seq = 3, 9 ... * > 95; # 3 9 15 21 27 [...] 81 87 93 99 +@numbers[5..*] = 3, 9 ... *; # even though the sequence is infinite, + # only the 15 needed values will be calculated. +say @numbers; #=> 0 1 2 3 4 3 9 15 21 [...] 81 87 + # (only 20 values) ## * And, Or 3 && 4; # 4, which is Truthy. Calls `.Bool` on `4` and gets `True`. @@ -418,7 +394,7 @@ $a && $b && $c; # Returns the first argument that evaluates to False, $a || $b; # And because you're going to want them, -# you also have composed assignment operators: +# you also have compound assignment operators: $a *= 2; # multiply and assignment $b %%= 5; # divisible by and assignment @array .= sort; # calls the `sort` method and assigns the result back @@ -428,7 +404,7 @@ $b %%= 5; # divisible by and assignment # a few more key concepts that make them better than in any other language :-). ## Unpacking ! -# It's the ability to "extract" arrays and keys. +# It's the ability to "extract" arrays and keys (AKA "destructuring"). # It'll work in `my`s and in parameter lists. my ($a, $b) = 1, 2; say $a; #=> 1 -- cgit v1.2.3 From 041064416115985ef336babe6ef7dbac726327fa Mon Sep 17 00:00:00 2001 From: Jonathan Scott Duff Date: Fri, 24 Apr 2015 17:07:55 -0500 Subject: Add a link about parrot suspension --- perl6.html.markdown | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index b2d7d48c..bcf56800 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -7,11 +7,13 @@ contributors: - ["Nami-Doc", "http://github.com/Nami-Doc"] --- -Perl 6 is a highly capable, feature-rich programming language made for the -upcoming hundred years. +Perl 6 is a highly capable, feature-rich programming language made for at +least the next hundred years. -Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM -and [the MoarVM](http://moarvm.com). +The primary Perl 6 compiler is called [Rakudo](http://rakudo.org), which runs on +the JVM and [the MoarVM](http://moarvm.com) and +[prior to March 2015](http://pmthium.com/2015/02/suspending-rakudo-parrot/), +[the Parrot VM](http://parrot.org/). Meta-note : the triple pound signs are here to denote headlines, double paragraphs, and single notes. -- cgit v1.2.3 From 483997e4340b84d96f6e14688484d5a6b96c616e Mon Sep 17 00:00:00 2001 From: ven Date: Sat, 9 May 2015 23:05:58 +0200 Subject: Fix #1094 --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index b2d7d48c..c3626057 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -211,7 +211,7 @@ say $x; #=> 52 # - `if` # Before talking about `if`, we need to know which values are "Truthy" # (represent True), and which are "Falsey" (or "Falsy") -- represent False. -# Only these values are Falsey: (), 0, "0", "", Nil, A type (like `Str` or `Int`), +# Only these values are Falsey: (), 0, "", Nil, A type (like `Str` or `Int`), # and of course False itself. # Every other value is Truthy. if True { -- cgit v1.2.3 From cbf2f6d371faf02e6fbe9c106e81340483817df6 Mon Sep 17 00:00:00 2001 From: David Warring Date: Wed, 13 May 2015 12:20:54 +1200 Subject: Parrot is no longer supported. Also mention MoarVM before JVM. --- perl6.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index c3626057..3bb87916 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -10,8 +10,7 @@ contributors: Perl 6 is a highly capable, feature-rich programming language made for the upcoming hundred years. -Perl 6 runs on [the Parrot VM](http://parrot.org/), the JVM -and [the MoarVM](http://moarvm.com). +Perl 6 runs on [the MoarVM](http://moarvm.com) and the JVM. Meta-note : the triple pound signs are here to denote headlines, double paragraphs, and single notes. -- cgit v1.2.3 From ed8f34eb3337bd78e2dca23be34746d01272f1ae Mon Sep 17 00:00:00 2001 From: ven Date: Sat, 27 Jun 2015 16:07:21 +0200 Subject: fix typo, resolves #1156 --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 44ad333c..de7d2f25 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -1044,7 +1044,7 @@ postcircumfix:<{ }>(%h, $key, :delete); # (you can call operators like that) # *everything* -- with great power comes great responsibility) ## Meta operators ! -# Oh boy, get ready. Get ready, because we're dwelving deep +# Oh boy, get ready. Get ready, because we're delving deep # into the rabbit's hole, and you probably won't want to go # back to other languages after reading that. # (I'm guessing you don't want to already at that point). -- cgit v1.2.3 From 45c70baa69088ca4aa9189556641a773b2b9c305 Mon Sep 17 00:00:00 2001 From: Aleks-Daniel Jakimenko Date: Tue, 25 Aug 2015 22:31:25 +0300 Subject: In perl6, 0 is not falsey anymore --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index de7d2f25..af545793 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -213,7 +213,7 @@ say $x; #=> 52 # - `if` # Before talking about `if`, we need to know which values are "Truthy" # (represent True), and which are "Falsey" (or "Falsy") -- represent False. -# Only these values are Falsey: (), 0, "", Nil, A type (like `Str` or `Int`), +# Only these values are Falsey: (), "", Nil, A type (like `Str` or `Int`), # and of course False itself. # Every other value is Truthy. if True { -- cgit v1.2.3 From 62f27af4c38f546f29fcf095fc653989b0667908 Mon Sep 17 00:00:00 2001 From: Jack Kuan Date: Wed, 23 Sep 2015 00:04:02 -0400 Subject: Update perl6.html.markdown --- perl6.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index af545793..8d425f7d 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -213,7 +213,7 @@ say $x; #=> 52 # - `if` # Before talking about `if`, we need to know which values are "Truthy" # (represent True), and which are "Falsey" (or "Falsy") -- represent False. -# Only these values are Falsey: (), "", Nil, A type (like `Str` or `Int`), +# Only these values are Falsey: 0, (), {}, "", Nil, A type (like `Str` or `Int`), # and of course False itself. # Every other value is Truthy. if True { -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- perl6.html.markdown | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'perl6.html.markdown') diff --git a/perl6.html.markdown b/perl6.html.markdown index 8d425f7d..63c0830a 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -7,11 +7,11 @@ contributors: - ["Nami-Doc", "http://github.com/Nami-Doc"] --- -Perl 6 is a highly capable, feature-rich programming language made for at +Perl 6 is a highly capable, feature-rich programming language made for at least the next hundred years. The primary Perl 6 compiler is called [Rakudo](http://rakudo.org), which runs on -the JVM and [the MoarVM](http://moarvm.com) and +the JVM and [the MoarVM](http://moarvm.com) and [prior to March 2015](http://pmthium.com/2015/02/suspending-rakudo-parrot/), [the Parrot VM](http://parrot.org/). @@ -143,7 +143,7 @@ sub with-mandatory-named(:$str!) { say "$str !"; } with-mandatory-named(str => "My String"); #=> My String ! -with-mandatory-named; # run time error: "Required named parameter not passed" +with-mandatory-named; # run time error: "Required named parameter not passed" with-mandatory-named(3); # run time error: "Too many positional parameters passed" ## If a sub takes a named boolean argument ... @@ -290,7 +290,7 @@ for @array -> $variable { # That means you can use `when` in a `for` just like you were in a `given`. for @array { say "I've got $_"; - + .say; # This is also allowed. # A dot call with no "topic" (receiver) is sent to `$_` by default $_.say; # the above and this are equivalent. @@ -378,8 +378,8 @@ say join(' ', @array[15..*]); #=> 15 16 17 18 19 # which is equivalent to: say join(' ', @array[-> $n { 15..$n }]); -# You can use that in most places you'd expect, even assigning to an array -my @numbers = ^20; +# You can use that in most places you'd expect, even assigning to an array +my @numbers = ^20; my @seq = 3, 9 ... * > 95; # 3 9 15 21 27 [...] 81 87 93 99 @numbers[5..*] = 3, 9 ... *; # even though the sequence is infinite, # only the 15 needed values will be calculated. @@ -634,14 +634,14 @@ class A { method get-value { $.field + $!private-field; } - + method set-value($n) { # $.field = $n; # As stated before, you can't use the `$.` immutable version. $!field = $n; # This works, because `$!` is always mutable. - + $.other-field = 5; # This works, because `$.other-field` is `rw`. } - + method !private-method { say "This method is private to the class !"; } @@ -660,19 +660,19 @@ $a.other-field = 10; # This, however, works, because the public field class A { has $.val; - + submethod not-inherited { say "This method won't be available on B."; say "This is most useful for BUILD, which we'll see later"; } - + method bar { $.val * 5 } } class B is A { # inheritance uses `is` method foo { say $.val; } - + method bar { $.val * 10 } # this shadows A's `bar` } @@ -699,20 +699,20 @@ role PrintableVal { # you "import" a mixin (a "role") with "does": class Item does PrintableVal { has $.val; - + # When `does`-ed, a `role` literally "mixes in" the class: # the methods and fields are put together, which means a class can access # the private fields/methods of its roles (but not the inverse !): method access { say $!counter++; } - + # However, this: # method print {} # is ONLY valid when `print` isn't a `multi` with the same dispatch. # (this means a parent class can shadow a child class's `multi print() {}`, # but it's an error if a role does) - + # NOTE: You can use a role as a class (with `is ROLE`). In this case, methods # will be shadowed, since the compiler will consider `ROLE` to be a class. } @@ -812,7 +812,7 @@ module Foo::Bar { say "Can't access me from outside, I'm my !"; } } - + say ++$n; # lexically-scoped variables are still available } say $Foo::Bar::n; #=> 1 @@ -1075,8 +1075,8 @@ say [//] Nil, Any, False, 1, 5; #=> False # Default value examples: -say [*] (); #=> 1 -say [+] (); #=> 0 +say [*] (); #=> 1 +say [+] (); #=> 0 # meaningless values, since N*1=N and N+0=N. say [//]; #=> (Any) # There's no "default value" for `//`. @@ -1335,7 +1335,7 @@ sub MAIN($name) { say "Hello, $name !" } # This produces: # $ perl6 cli.pl # Usage: -# t.pl +# t.pl # And since it's a regular Perl 6 sub, you can haz multi-dispatch: # (using a "Bool" for the named argument so that we can do `--replace` @@ -1348,7 +1348,7 @@ multi MAIN('import', File, Str :$as) { ... } # omitting parameter name # This produces: # $ perl 6 cli.pl # Usage: -# t.pl [--replace] add +# t.pl [--replace] add # t.pl remove # t.pl [--as=] import (File) # As you can see, this is *very* powerful. @@ -1400,7 +1400,7 @@ for { # (explained in details below). .say } - + if rand == 0 ff rand == 1 { # compare variables other than `$_` say "This ... probably will never run ..."; } -- cgit v1.2.3