diff options
Diffstat (limited to 'perl6.html.markdown')
-rw-r--r-- | perl6.html.markdown | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/perl6.html.markdown b/perl6.html.markdown index 13f383fe..8404f9f8 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, @@ -202,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 @@ -254,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) @@ -284,7 +285,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 $_"; @@ -561,7 +562,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" @@ -653,7 +654,7 @@ class A { has Int $!private-field = 10; method get-value { - $.field + $!private-field + $n; + $.field + $!private-field; } method set-value($n) { @@ -671,7 +672,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`). @@ -964,7 +965,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 !! |