mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-07-31 12:00:34 +02:00
Update perl6.html.markdown
This commit is contained in:
@@ -440,6 +440,7 @@ say @array[^10]; # you can pass arrays as subscripts and it'll return
|
|||||||
# Note : when reading an infinite list, Perl 6 will "reify" the elements
|
# 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.
|
# it needs, then keep them in memory. They won't be calculated more than once.
|
||||||
# It also will never calculate more elements that are needed.
|
# It also will never calculate more elements that are needed.
|
||||||
|
# Trying
|
||||||
|
|
||||||
# An array subscript can also be a closure.
|
# An array subscript can also be a closure.
|
||||||
# It'll be called with the length as the argument
|
# It'll be called with the length as the argument
|
||||||
@@ -961,10 +962,9 @@ try {
|
|||||||
use JSON::Tiny; # if you installed Rakudo* or Panda, you'll have this module
|
use JSON::Tiny; # if you installed Rakudo* or Panda, you'll have this module
|
||||||
say from-json('[1]').perl; #=> [1]
|
say from-json('[1]').perl; #=> [1]
|
||||||
|
|
||||||
# Declare your own packages like this:
|
# You should not declare packages using the `package` keyword (unlike Perl 5).
|
||||||
# `class Package::Name::Here;` to declare a class, or if you only want to
|
# Instead, use `class Package::Name::Here;` to declare a class, or if you only want to
|
||||||
# export variables/subs, you can use `module`. If you're coming from Perl 5
|
# export variables/subs, you can use `module`.
|
||||||
# please note you're not usually supposed to use the `package` keyword.
|
|
||||||
|
|
||||||
module Hello::World { # Bracketed form
|
module Hello::World { # Bracketed form
|
||||||
# If `Hello` doesn't exist yet, it'll just be a "stub",
|
# If `Hello` doesn't exist yet, it'll just be a "stub",
|
||||||
@@ -1073,15 +1073,23 @@ ENTER { say "[*] Runs everytime you enter a block, repeats on loop blocks" }
|
|||||||
LEAVE { say "Runs everytime you leave a block, even when an exception
|
LEAVE { say "Runs everytime you leave a block, even when an exception
|
||||||
happened. Repeats on loop blocks." }
|
happened. Repeats on loop blocks." }
|
||||||
|
|
||||||
PRE { say "Asserts a precondition at every block entry,
|
PRE {
|
||||||
before ENTER (especially useful for loops)" }
|
say "Asserts a precondition at every block entry,
|
||||||
|
before ENTER (especially useful for loops)";
|
||||||
|
say "If this block doesn't return a truthy value,
|
||||||
|
an exception of type X::Phaser::PrePost is thrown.";
|
||||||
|
}
|
||||||
# exemple:
|
# exemple:
|
||||||
for 0..2 {
|
for 0..2 {
|
||||||
PRE { $_ > 1 } # This is going to blow up with "Precondition failed"
|
PRE { $_ > 1 } # This is going to blow up with "Precondition failed"
|
||||||
}
|
}
|
||||||
|
|
||||||
POST { say "Asserts a postcondition at every block exit,
|
POST {
|
||||||
after LEAVE (especially useful for loops)" }
|
say "Asserts a postcondition at every block exit,
|
||||||
|
after LEAVE (especially useful for loops)";
|
||||||
|
say "If this block doesn't return a truthy value,
|
||||||
|
an exception of type X::Phaser::PrePost is thrown, like PRE.";
|
||||||
|
}
|
||||||
for 0..2 {
|
for 0..2 {
|
||||||
POST { $_ < 2 } # This is going to blow up with "Postcondition failed"
|
POST { $_ < 2 } # This is going to blow up with "Postcondition failed"
|
||||||
}
|
}
|
||||||
@@ -1522,14 +1530,17 @@ say $0; # The same as above.
|
|||||||
# IFF it can have more than one element
|
# IFF it can have more than one element
|
||||||
# (so, with `*`, `+` and `**` (whatever the operands), but not with `?`).
|
# (so, with `*`, `+` and `**` (whatever the operands), but not with `?`).
|
||||||
# Let's use examples to see that:
|
# Let's use examples to see that:
|
||||||
so 'fooABCbar' ~~ / foo ( A B C )? bar /; # `True`
|
|
||||||
|
# Note: We quoted A B C to demonstrate that the whitespace between them isn't significant.
|
||||||
|
# If we want the whitespace to *be* significant there, we can use the :sigspace modifier.
|
||||||
|
so 'fooABCbar' ~~ / foo ( "A" "B" "C" )? bar /; # `True`
|
||||||
say $/[0]; #=> 「ABC」
|
say $/[0]; #=> 「ABC」
|
||||||
say $0.WHAT; #=> (Match)
|
say $0.WHAT; #=> (Match)
|
||||||
# It can't be more than one, so it's only a single match object.
|
# There can't be more than one, so it's only a single match object.
|
||||||
so 'foobar' ~~ / foo ( A B C )? bar /; #=> True
|
so 'foobar' ~~ / foo ( "A" "B" "C" )? bar /; #=> True
|
||||||
say $0.WHAT; #=> (Any)
|
say $0.WHAT; #=> (Any)
|
||||||
# This capture did not match, so it's empty
|
# This capture did not match, so it's empty
|
||||||
so 'foobar' ~~ / foo ( A B C ) ** 0..1 bar /; # `True`
|
so 'foobar' ~~ / foo ( "A" "B" "C" ) ** 0..1 bar /; # `True`
|
||||||
say $0.WHAT; #=> (Array)
|
say $0.WHAT; #=> (Array)
|
||||||
# A specific quantifier will always capture an Array,
|
# A specific quantifier will always capture an Array,
|
||||||
# may it be a range or a specific value (even 1).
|
# may it be a range or a specific value (even 1).
|
||||||
|
Reference in New Issue
Block a user