diff options
| author | Nami-Doc <vendethiel@hotmail.fr> | 2014-07-26 00:37:36 +0200 | 
|---|---|---|
| committer | Nami-Doc <vendethiel@hotmail.fr> | 2014-07-26 00:37:36 +0200 | 
| commit | 61351ee266141d57ee83fe5be08f480cf2de3969 (patch) | |
| tree | deebfbbddc3572833e888aa06ead86dcf6f26278 | |
| parent | 2347d375d226df1e1b7f77043e272ff0b583c5a2 (diff) | |
Fix explanations about $_
@masak++
| -rw-r--r-- | perl6.html.markdown | 18 | 
1 files changed, 15 insertions, 3 deletions
| 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 { | 
