mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +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('You'); #=> Hello, You !
|
||||
|
||||
## You can also, by using a syntax akin to the one of hashes (yay unified syntax !),
|
||||
## pass *named* arguments to a `sub`.
|
||||
## You can also, by using a syntax akin to the one of hashes
|
||||
## (yay unified syntax !), pass *named* arguments to a `sub`.
|
||||
# They're optional, and will default to "Any".
|
||||
sub with-named($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; # 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 ...
|
||||
sub takes-a-bool($name, :$bool) {
|
||||
@ -169,9 +169,9 @@ my &s = &say-hello;
|
||||
my &other-s = sub { say "Anonymous function !" }
|
||||
|
||||
# A sub can have a "slurpy" parameter, or "doesn't-matter-how-many"
|
||||
sub as-many($head, *@rest) { # `*@` (slurpy) will basically "take everything else".
|
||||
# Note: you can have parameters *before* (like here)
|
||||
# a slurpy one, but not *after*.
|
||||
sub as-many($head, *@rest) { #`*@` (slurpy) will "take everything else"
|
||||
# Note: you can have parameters *before* a slurpy one (like here),
|
||||
# but not *after*.
|
||||
say @rest.join(' / ') ~ " !";
|
||||
}
|
||||
say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday !
|
||||
@ -224,7 +224,7 @@ say $x; #=> 52
|
||||
# - `if`
|
||||
# Before talking about `if`, we need to know which values are "Truthy"
|
||||
# (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.
|
||||
# Every other value is Truthy.
|
||||
if True {
|
||||
@ -265,13 +265,14 @@ say $age > 18 ?? "You are an adult" !! "You are under 18";
|
||||
|
||||
given "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/`.
|
||||
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.
|
||||
# This when is equivalent to this `if`:
|
||||
# This `when` is equivalent to this `if`:
|
||||
# if $_ ~~ ($_.chars > 50) {...}
|
||||
# Which means:
|
||||
# if $_.chars > 50 {...}
|
||||
@ -288,7 +289,8 @@ given "foo bar" {
|
||||
# but can also be a C-style `for` loop:
|
||||
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++) {
|
||||
|
Loading…
Reference in New Issue
Block a user