In Regexes§
See primary documentation in context for Start of string and end of string
The ^
anchor only matches at the start of the string:
say so 'karakul' ~~ / raku/; # OUTPUT: «True»say so 'karakul' ~~ /^ raku/; # OUTPUT: «False»say so 'rakuy' ~~ /^ raku/; # OUTPUT: «True»say so 'raku' ~~ /^ raku/; # OUTPUT: «True»
The $
anchor only matches at the end of the string:
say so 'use raku' ~~ / raku /; # OUTPUT: «True»say so 'use raku' ~~ / raku $/; # OUTPUT: «True»say so 'rakuy' ~~ / raku $/; # OUTPUT: «False»
You can combine both anchors:
say so 'use raku' ~~ /^ raku $/; # OUTPUT: «False»say so 'raku' ~~ /^ raku $/; # OUTPUT: «True»
Keep in mind that ^
matches the start of a string, not the start of a line. Likewise, $
matches the end of a string, not the end of a line.
The following is a multi-line string:
my = chomp# 'safe' is at the end of the stringsay so ~~ /safe $/; # OUTPUT: «True»# 'secret' is at the end of a line, not the stringsay so ~~ /secret $/; # OUTPUT: «False»# 'Keep' is at the start of the stringsay so ~~ /^Keep /; # OUTPUT: «True»# 'and' is at the start of a line -- not the stringsay so ~~ /^and /; # OUTPUT: «False»
In Variables§
See primary documentation in context for The ^ twigil
The ^
twigil declares a formal positional parameter to blocks or subroutines; that is, variables of the form $^variable
are a type of placeholder variable. They may be used in bare blocks to declare formal parameters to that block. So the block in the code
my = 1,3,9…100;say reduce , 0, |;# OUTPUT: «61»
has two formal parameters, namely $a
and $b
. Note that even though $^b
appears before $^a
in the code, $^a
is still the first formal parameter to that block. This is because the placeholder variables are sorted in Unicode order.
Although it is possible to use nearly any valid identifier as a placeholder variable, it is recommended to use short names or ones that can be trivially understood in the correct order, to avoid surprise on behalf of the reader.
Normal blocks and subroutines may also make use of placeholder variables but only if they do not have an explicit parameter list.
sub say-it # validsub say-it() # invalid# valid-> , , # invalid
Placeholder variables cannot have type constraints or a variable name with a single uppercase letter (this is disallowed to enable catching some Perl-isms).
The ^
twigil can be combined with any sigil to create a placeholder variable with that sigil. The sigil will have its normal semantic effects, as described in the Sigils table. Thus @^array
, %^hash
, and &^fun
are all valid placeholder variables.
In Traps to avoid§
See primary documentation in context for The ^ twigil
Using the ^
twigil can save a fair amount of time and space when writing out small blocks of code. As an example:
for 1..8 -> ,
can be shortened to just
for 1..8
The trouble arises when a person wants to use more complex names for the variables, instead of just one letter. The ^
twigil is able to have the positional variables be out of order and named whatever you want, but assigns values based on the variable's Unicode ordering. In the above example, we can have $^a
and $^b
switch places, and those variables will keep their positional values. This is because the Unicode character 'a' comes before the character 'b'. For example:
# In ordersub f1f1 "Hello", "there"; # OUTPUT: «Hello there»
# Out of ordersub f2f2 "Hello", "there"; # OUTPUT: «there Hello»
Due to the variables allowed to be called anything, this can cause some problems if you are not accustomed to how Raku handles these variables.
# BAD NAMING: alphabetically `four` comes first and gets value `1` in it:for 1..4 # OUTPUT: «2 4 3 1»# GOOD NAMING: variables' naming makes it clear how they sort alphabetically:for 1..4 # OUTPUT: «1 2 3 4»