diff options
Diffstat (limited to 'perl6.html.markdown')
| -rw-r--r-- | perl6.html.markdown | 566 | 
1 files changed, 348 insertions, 218 deletions
| diff --git a/perl6.html.markdown b/perl6.html.markdown index fca863af..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. @@ -30,12 +29,13 @@ 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 `$`  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 : @@ -46,24 +46,33 @@ 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 = <a b c>; # array of words, delimited by space. +my @letters = <a b c>; # 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 +say "Interpolate an array using [] : @array[]"; +#=> 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 6 b -## * 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 = <key1 value1 key2 value2>; # you can also create a hash @@ -93,30 +102,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) { # 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 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, @@ -135,23 +120,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!)  { @@ -168,22 +150,37 @@ 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) {    say $def;  }  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". @@ -191,7 +188,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,25 +196,21 @@ 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 -            # (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 `x-store` 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`  # 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 { @@ -228,30 +221,40 @@ 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/ { # You'll read about the smart-matching operator below -- 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) {...} +                       # Which means: +                       #  if $_.chars > 50 {...}      say "Quite a long string !";    }    default { # same as `when *` (using the Whatever Star) @@ -262,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 @@ -270,18 +273,18 @@ 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 !";  }  # - `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 `$_`. -# 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 $_"; @@ -292,28 +295,29 @@ 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` : +# 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";  }  ### 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). -## 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 +338,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` @@ -343,9 +348,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; @@ -357,18 +359,29 @@ $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 !)  # 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. +# 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`. @@ -380,7 +393,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 @@ -390,7 +403,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 @@ -415,7 +428,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 +498,8 @@ 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 }` +                                     # also `sub ($a, $b) { $a + $b + 3 }`  say (*/2)(4); #=> 2                # Immediatly execute the function Whatever created.  say ((*+3)/5)(5); #=> 1.6 @@ -494,11 +508,27 @@ 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 }` +## 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`: @@ -507,20 +537,19 @@ map({ $^a + $^b + 3 }, @array); # same as the above  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. @@ -576,7 +605,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 +618,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. @@ -599,7 +629,7 @@ class A {    has Int $!private-field = 10;    method get-value { -    $.field + $!private-field + $n; +    $.field + $!private-field;    }    method set-value($n) { @@ -617,7 +647,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`). @@ -663,7 +693,7 @@ role PrintableVal {    }  } -# you "use" a mixin with "does" : +# you "import" a mixin (a "role") with "does":  class Item does PrintableVal {    has $.val; @@ -685,7 +715,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 {} }`). @@ -695,21 +725,43 @@ 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  #  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 +771,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 +893,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  } @@ -888,7 +940,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 !! @@ -951,7 +1003,7 @@ say 5!; #=> 120  sub infix:<times>(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 @@ -1000,12 +1052,11 @@ 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) 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 @@ -1023,9 +1074,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 `//`. @@ -1065,99 +1114,20 @@ 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) +# $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.  # 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; #=> 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 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 <well met young hero we shall meet later> { -  # 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 <A B C B A>; #=> 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 <A B C B A>; #=> B C B -                                    # 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 <test start print this stop you stopped printing start printing again stop not anymore> { -  .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 <a b c> { -  .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. @@ -1190,8 +1160,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,9 +1203,9 @@ 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". +# - `**` - (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) @@ -1244,6 +1214,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 `(?:)`). @@ -1255,7 +1246,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 `$/`: @@ -1271,7 +1262,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」 @@ -1285,30 +1276,68 @@ 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<key>` 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 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+     [ <subrule1> || <subrule2> ] /; +# 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  #  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:  #      t.pl <name>   # 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, 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) { ... } @@ -1322,10 +1351,111 @@ 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 <well met young hero we shall meet later> { +  # 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 <A B C B A>; #=> 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 <A B C B A>; #=> 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 <test start print it stop not printing start print again stop not anymore> { +  .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 <a b c> { +  .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: +   - 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. + | 
