diff options
Diffstat (limited to 'perl6.html.markdown')
-rw-r--r-- | perl6.html.markdown | 86 |
1 files changed, 33 insertions, 53 deletions
diff --git a/perl6.html.markdown b/perl6.html.markdown index 72faecb6..af545793 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. @@ -201,7 +203,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 @@ -211,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, "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 { @@ -253,7 +255,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) @@ -283,7 +287,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 $_"; @@ -305,37 +309,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). @@ -394,17 +370,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`. @@ -416,7 +396,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 @@ -426,7 +406,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 @@ -560,7 +540,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" @@ -963,7 +943,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 !! @@ -1064,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). |