mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 10:01:38 +00:00
Fit some more things into 80 columns in the Perl 6 doc
This commit is contained in:
parent
6e00874000
commit
7f0fff0adf
@ -125,8 +125,8 @@ hello-to; #=> Hello, World !
|
|||||||
hello-to(); #=> Hello, World !
|
hello-to(); #=> Hello, World !
|
||||||
hello-to('You'); #=> Hello, You !
|
hello-to('You'); #=> Hello, You !
|
||||||
|
|
||||||
## You can also, by using a syntax akin to the one of hashes (yay unified syntax !),
|
## You can also, by using a syntax akin to the one of hashes
|
||||||
## pass *named* arguments to a `sub`.
|
## (yay unified syntax !), pass *named* arguments to a `sub`.
|
||||||
# They're optional, and will default to "Any".
|
# They're optional, and will default to "Any".
|
||||||
sub with-named($normal-arg, :$named) {
|
sub with-named($normal-arg, :$named) {
|
||||||
say $normal-arg + $named;
|
say $normal-arg + $named;
|
||||||
@ -146,7 +146,7 @@ sub with-mandatory-named(:$str!) {
|
|||||||
}
|
}
|
||||||
with-mandatory-named(str => "My String"); #=> My String !
|
with-mandatory-named(str => "My String"); #=> My String !
|
||||||
with-mandatory-named; # run time error: "Required named parameter not passed"
|
with-mandatory-named; # run time error: "Required named parameter not passed"
|
||||||
with-mandatory-named(3); # run time error: "Too many positional parameters passed"
|
with-mandatory-named(3);# run time error:"Too many positional parameters passed"
|
||||||
|
|
||||||
## If a sub takes a named boolean argument ...
|
## If a sub takes a named boolean argument ...
|
||||||
sub takes-a-bool($name, :$bool) {
|
sub takes-a-bool($name, :$bool) {
|
||||||
@ -169,9 +169,9 @@ my &s = &say-hello;
|
|||||||
my &other-s = sub { say "Anonymous function !" }
|
my &other-s = sub { say "Anonymous function !" }
|
||||||
|
|
||||||
# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many"
|
# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many"
|
||||||
sub as-many($head, *@rest) { # `*@` (slurpy) will basically "take everything else".
|
sub as-many($head, *@rest) { #`*@` (slurpy) will "take everything else"
|
||||||
# Note: you can have parameters *before* (like here)
|
# Note: you can have parameters *before* a slurpy one (like here),
|
||||||
# a slurpy one, but not *after*.
|
# but not *after*.
|
||||||
say @rest.join(' / ') ~ " !";
|
say @rest.join(' / ') ~ " !";
|
||||||
}
|
}
|
||||||
say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday !
|
say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday !
|
||||||
@ -224,7 +224,7 @@ say $x; #=> 52
|
|||||||
# - `if`
|
# - `if`
|
||||||
# Before talking about `if`, we need to know which values are "Truthy"
|
# Before talking about `if`, we need to know which values are "Truthy"
|
||||||
# (represent True), and which are "Falsey" (or "Falsy") -- represent False.
|
# (represent True), and which are "Falsey" (or "Falsy") -- represent False.
|
||||||
# Only these values are Falsey: 0, (), {}, "", Nil, A type (like `Str` or `Int`),
|
# Only these values are Falsey: 0, (), {}, "", Nil, A type (like `Str` or `Int`)
|
||||||
# and of course False itself.
|
# and of course False itself.
|
||||||
# Every other value is Truthy.
|
# Every other value is Truthy.
|
||||||
if True {
|
if True {
|
||||||
@ -265,13 +265,14 @@ say $age > 18 ?? "You are an adult" !! "You are under 18";
|
|||||||
|
|
||||||
given "foo bar" {
|
given "foo bar" {
|
||||||
say $_; #=> foo bar
|
say $_; #=> foo bar
|
||||||
when /foo/ { # Don't worry about smart matching yet – just know `when` uses it.
|
when /foo/ { # Don't worry about smart matching yet – just know `when` uses it
|
||||||
# This is equivalent to `if $_ ~~ /foo/`.
|
# This is equivalent to `if $_ ~~ /foo/`.
|
||||||
say "Yay !";
|
say "Yay !";
|
||||||
}
|
}
|
||||||
when $_.chars > 50 { # smart matching anything with True (`$a ~~ True`) is True,
|
when $_.chars > 50 { # smart matching anything with True is True,
|
||||||
|
# i.e. (`$a ~~ True`)
|
||||||
# so you can also put "normal" conditionals.
|
# so you can also put "normal" conditionals.
|
||||||
# This when is equivalent to this `if`:
|
# This `when` is equivalent to this `if`:
|
||||||
# if $_ ~~ ($_.chars > 50) {...}
|
# if $_ ~~ ($_.chars > 50) {...}
|
||||||
# Which means:
|
# Which means:
|
||||||
# if $_.chars > 50 {...}
|
# if $_.chars > 50 {...}
|
||||||
@ -288,7 +289,8 @@ given "foo bar" {
|
|||||||
# but can also be a C-style `for` loop:
|
# but can also be a C-style `for` loop:
|
||||||
loop {
|
loop {
|
||||||
say "This is an infinite loop !";
|
say "This is an infinite loop !";
|
||||||
last; # last breaks out of the loop, like the `break` keyword in other languages
|
last; # last breaks out of the loop, like the `break` keyword in other
|
||||||
|
# languages
|
||||||
}
|
}
|
||||||
|
|
||||||
loop (my $i = 0; $i < 5; $i++) {
|
loop (my $i = 0; $i < 5; $i++) {
|
||||||
|
Loading…
Reference in New Issue
Block a user