I'm collecting all the ways you can use : in Perl 6.
Larry's 1st Law of Language Redesign: Everyone wants the colon
Larry's 2nd Law of Language Redesign: Larry gets the colon
A lot of these are variations on a theme, especially the theme of "colon-pair" syntax. Where two uses of the colon look different or are in a different usage context, even if they are technically the same, I'd prefer to illustrate them both.
# namespace
package A::B { ... }
class A::B { ... }
Namespace separatormy $x = A::B.new;
Dynamic namespacemy $package_name = "A::B";
my $x = ::($package_name).new;
Pseudopackage representing null namespace?say :: ;
Anonymous classmy $anon_class = class :: is Int {...}
Current class in a compile-time varclass Who { method myname { say ::?CLASS } }
Call Int method of Int class42.Int::Int;
Treat package X as a hash
say X::.keys;
# Binding
my $y := $a;
Read-only bindingmy $z ::= $y;
Container identity
$y =:= $a; # True
# Colon-Pair syntax (often seen as adverbs)
my $x = 'cat';
:foo(5) # 'foo' => 5
:foo('dog') # 'foo' => 'dog'
:foo($x) # 'foo' => 'cat'
Value word quoting, no interpolation:foo # 'foo' => 'bar'
:foo # 'foo' => ('bar', 'baz')
:foo<$x> # 'foo' => '$x'
Value word quoting with interpolation:foo<<$x>> # 'foo' => 'cat'
:foo<<$x dog>> # 'foo' => ('cat', 'dog')
Numeric-prefix extractionsay (:73day) # day => 73
Default to true valuesay (:foo) # foo => True
False instead of truesay (:!foo) # foo => False
Var name as key, content as valuemy $x = 'cat';
my @foo=1,2;
:@foo # foo => [1,2]
:$x # x => 'cat'
Base conversion via colon-pairsay :16("dead") # 57005
binding pair in sig:
my $a;
sub b (:foo($x)! is rw) { $x = 42};
b(:foo($a));
say $a # 42
# T takes value's type
-> Numeric ::T \x { say T }(42);
Type Smileys
Int:D; # Defined Int
Int:U; # Undefined Int
# signature literal
my $sig = :(Int $foo);
signature on Callable &var, not yet implemented
my &f:(Str) = -> Str {};
my $a = 1;
$a.infix:["+"](3); # $a + 3
$a.infix:<+>(3); # $a + 3, but shorter with quote brackets
$a.prefix:<++>; # ++$a
$a.postfix:<++>; # ++$a
$a.:<++>; # Shorthand for prefix (no param)
$a.:<+>(3); # Shorthand for infix (param provided)
Regex adverb:i/a/ # Case insensitive, outside
/:i a/ # Case insensitive, inside
Declare vars in regex scopemy regex foo { :my $var; }
Backtracking control
similar ones not yet implemented are :: ::> :::/ a: b?: c*: /
One especially interesting example you often (don't) see is :?/ .:? / # explicit minimal match
/ .? / # short version that is usually used has no colon
Unicode character classes
say so "a" ~~ /<[:Alpha]>/
# Quote adverb
Q:w(word of the day); \# :w adverb
Precedence dropper@stuff.map({ $_ + 1 }); # Explicit parameter
@stuff.map: { $_ + 1 }; # Parameters come after ':'
Invocant marker method invocation$x.say-hi(names => []); # Normal way
say-hi($x: names => []) # Invocant marker way
Declare alternative to 'self' for current-instance in methodmethod wuzzup($this: $name) { say "WUZZUP $name?!" }
object hashes, ones with complex keysmy $x = :{ (now) => "when?" };
longnamesub infix: { ... }
Specific uses of longnamesuse Foo:from;
Loop labelsMYLABEL: for ^100 {
for ^200 {
last MYLABEL;
}
}
Twigil for formal named param for a block (like $^x)say { $:add ?? $^a + $^b !! $^a - $^b }( 4, 5 ) :!add
This one is a stretch...
Colons instead of dashes when you invoke cli commands that use MAINsub MAIN ( :$filename ) { ... }
cmd --filename=file.txt
cmd :filename=file.txt
cmd :filename # filename => true