mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
parent
5b7ffeb77d
commit
341066bb86
@ -1,10 +1,9 @@
|
||||
---
|
||||
name: perl6
|
||||
category: language
|
||||
language: perl6
|
||||
filename: learnperl6.pl
|
||||
contributors:
|
||||
- ["Nami-Doc", "http://github.com/Nami-Doc"]
|
||||
- ["vendethiel", "http://github.com/vendethiel"]
|
||||
---
|
||||
|
||||
Perl 6 is a highly capable, feature-rich programming language made for at
|
||||
@ -374,6 +373,8 @@ say @array[^10]; # you can pass arrays as subscripts and it'll return
|
||||
say join(' ', @array[15..*]); #=> 15 16 17 18 19
|
||||
# which is equivalent to:
|
||||
say join(' ', @array[-> $n { 15..$n }]);
|
||||
# Note: if you try to do either of those with an infinite loop,
|
||||
# you'll trigger an infinite loop (your program won't finish)
|
||||
|
||||
# You can use that in most places you'd expect, even assigning to an array
|
||||
my @numbers = ^20;
|
||||
@ -763,8 +764,9 @@ try {
|
||||
# and `enum`) are actually packages. (Packages are the lowest common denominator)
|
||||
# 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`:
|
||||
# You're not supposed to use the package keyword, usually:
|
||||
# you use `class Package::Name::Here;` to declare a class,
|
||||
# or if you only want to export variables/subs, you can use `module`:
|
||||
module Hello::World { # Bracketed form
|
||||
# If `Hello` doesn't exist yet, it'll just be a "stub",
|
||||
# that can be redeclared as something else later.
|
||||
@ -774,11 +776,6 @@ unit module Parse::Text; # file-scoped form
|
||||
grammar Parse::Text::Grammar { # A grammar is a package, which you could `use`
|
||||
}
|
||||
|
||||
# NOTE for Perl 5 users: even though the `package` keyword exists,
|
||||
# the braceless form is invalid (to catch a "perl5ism"). This will error out:
|
||||
# package Foo; # because Perl 6 will think the entire file is Perl 5
|
||||
# Just use `module` or the brace version of `package`.
|
||||
|
||||
# You can use a module (bring its declarations into scope) with `use`
|
||||
use JSON::Tiny; # if you installed Rakudo* or Panda, you'll have this module
|
||||
say from-json('[1]').perl; #=> [1]
|
||||
@ -870,8 +867,16 @@ LEAVE { say "Runs everytime you leave a block, even when an exception
|
||||
|
||||
PRE { say "Asserts a precondition at every block entry,
|
||||
before ENTER (especially useful for loops)" }
|
||||
# exemple:
|
||||
for 0..2 {
|
||||
PRE { $_ > 1 } # This is going to blow up with "Precondition failed"
|
||||
}
|
||||
|
||||
POST { say "Asserts a postcondition at every block exit,
|
||||
after LEAVE (especially useful for loops)" }
|
||||
for 0..2 {
|
||||
POST { $_ < 2 } # This is going to blow up with "Postcondition failed"
|
||||
}
|
||||
|
||||
## * Block/exceptions phasers
|
||||
sub {
|
||||
@ -1239,14 +1244,14 @@ so 'foo!' ~~ / <-[ a..z ] + [ f o ]> + /; # True (the + doesn't replace the left
|
||||
# Group: you can group parts of your regexp with `[]`.
|
||||
# These groups are *not* captured (like PCRE's `(?:)`).
|
||||
so 'abc' ~~ / a [ b ] c /; # `True`. The grouping does pretty much nothing
|
||||
so 'fooABCABCbar' ~~ / foo [ A B C ] + bar /;
|
||||
so 'foo012012bar' ~~ / foo [ '01' <[0..9]> ] + bar /;
|
||||
# The previous line returns `True`.
|
||||
# We match the "ABC" 1 or more time (the `+` was applied to the group).
|
||||
# We match the "012" 1 or more time (the `+` was applied to the group).
|
||||
|
||||
# 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`. (using `so` here, `$/` below)
|
||||
so 'fooABCABCbar' ~~ / foo ( 'A' <[A..Z]> 'C' ) + bar /; # `True`. (using `so` here, `$/` below)
|
||||
|
||||
# So, starting with the grouping explanations.
|
||||
# As we said before, our `Match` object is available as `$/`:
|
||||
|
Loading…
Reference in New Issue
Block a user