Raku Programming/Variables and Data
Static and Dynamic Programming Languages
editRaku is one of a class of programming languages called dynamic languages. Dynamic languages use variables whose data type can change at runtime and don't need to be predeclared. The alternative to dynamic languages are static languages, such as C or Java, where variables must typically be declared as a specific data type before they are used.
In a language like C or one of its derivatives (C++, C# or Java for instance) variables need to be predeclared with a type before they can be used:
unsigned short int x;
x = 10;
The code above isn't entirely accurate since in C you can initialize a variable when you declare it:
unsigned short int x = 10;
However, the variable x
cannot be used before it's declared. Once it's declared as the type unsigned short int
, you can't use x
to store other types of data like floating point numbers or data pointers, at least not without explicit coercion:
unsigned short int x;
x = 1.02; /* Wrong! */
unsigned short int y;
x = &y; /* Wrong! */
In dynamic programming languages like Raku, variables can be automatically allocated when they are first used without having to be explicitly declared. Also, variables in Raku are polymorphic: They can be integers, strings, floating point numbers, or complex data structures like arrays or hashes without any coercion. Here are some examples:
my $x;
$x = 5; # Integer
$x = "hello"; # String
$x = 3.1415; # Floating Point Number
The example above demonstrates a number of different ideas that we will discuss throughout the rest of this book. One important idea is Comments. Comments are notes in the source code that are intended to be read by the programmers and are ignored by the Raku interpreter. In Raku, most comments are marked with a #
symbol, and continue until the end of the line. Raku also has embedded comments and multi-line documentation, which we will talk about later.
We haven't been entirely honest about Raku data. Raku does allow data to be given an explicit type, if you want it to. The default is to use data that is polymorphic like we've seen above, but you can also declare that a scalar may only hold an integer, or a string, or a number, or a different data item all together. We'll talk more about Raku's explicit type system later. For now, it's easier to think that data in Raku doesn't have explicit types (or, more specifically, that it doesn't need them).
The examples we see above also show an important keyword: my
. my
is used to declare a new variable for use. We'll talk more about my
and its uses later.
Variables and Sigils
editAs we saw in the brief example above, Raku variables have symbols in front of them called sigils. Sigils serve a number of important uses, but one of the most important is to establish context. Raku has four types of sigils that are used to establish different types of data. The $ sign that we saw above is for scalars: single data values like numbers, strings, or object references. Other sigils to be used are @ which denotes arrays of data, % for hashes of data, and & for subroutines or executable code blocks.
- Scalars
- Scalars as we have already seen contain a single data item like a number or a string.
- Arrays
- Arrays are lists of data of the same type that are indexed by number.
- Hashes
- Hashes are sets of data of potentially different types of data indexed by a string.
- Code References
- Code references are pointers to executable code structures that can be passed around like data and called at different places in your code.
Scalars
editWe've seen some uses of scalars above, here we're going to show a slightly more comprehensive list.
my $x;
$x = 42; # Decimal Integer
$x = 0xF6; # Hexadecimal Integer
$x = 0b1010010001; # Binary Integer
$x = 3.1415; # Floating Point Number
$x = 2.34E-5; # Scientific Notation
$x = "Hello "; # Double-Quoted String
$x = 'World!'; # Single-Quoted String
$x = q:to/EOS/; # Heredoc string
This is a heredoc string. It starts at the "q:to"
term and continues until we reach the terminator
specified in quotes above. This is useful for
large multi-line string literals. We will talk about
heredocs in more detail later
EOS
$x = MyObject.new(); # Object Reference
Scalars are the most fundamental and basic type of data in Raku and are the ones that are probably going to be used most often in your programs. This is because they are so versatile.
Arrays
editArrays, as we mentioned above, are lists of data objects that are considered to be the same type. Since arrays are lists of scalars, it's possible for some elements in an array to be numbers and some to be strings and some to be other data entirely. However, this is generally not considered to be the best use of arrays.
Arrays are prefixed with the @ sigil, and can be indexed using integers in [ square brackets ]. Here are some examples of using arrays:
my @a;
@a = 1, 2, 3;
@a = "first", "second", "third";
@a = 1.2, 3.14, 2.717;
Once we have an array we can extract the scalar data items out of them using index notation:
my @a, $x;
@a = "first", "second", "third";
$x = @a[0]; # first
$x = @a[1]; # second
$x = @a[2]; # third
Arrays can also be multi-dimensional:
my @a;
@a[0, 0] = 1;
@a[0, 1] = 2;
@a[1, 0] = 3;
@a[1, 1] = 4;
# @a is now:
# |1, 2|
# |3, 4|
Arrays also don't have to just store scalars, they can store any other data items as well:
my @a, @b, @c, @d, %e, %f, %g, &h, &i, &j;
@a = @b, @c, @d;
@a = %e, %f, %g;
@a = &h, &i, &j;
This can be the basis of some complex data structures, and we'll talk more about composing structures like this later.
Hashes
editHashes are similar in many ways to arrays: They can contain a group of objects. However, unlike arrays hashes use names for their items instead of numbers. Here are some examples:
my %a = "first" => 1, "second" => 2, "third" => 3;
my $x = %a{"first"}; # 1
my $y = %a{"second"}; # 2
my $z = %a{"third"}; # 3
The special =>
symbol is similar to a comma except it creates a pair. A pair is a combination of a string name and an associated data object. Hashes can sometimes be thought of as being arrays of pairs. Notice also that hashes use curly brackets to index their data instead of square brackets like arrays use.
Hashes can also use a special syntax called autoquoting to help make looking up hash values easier. Instead of using the curly brackets and quotes {" "}
, you can use the angle brackets < >
by themselves to do the same job:
my %a = "foo" => "first", "bar" => "second";
my $x = %a{"foo"}; # "first"
my $y = %a<bar> # "second"
Adverb Syntax
editPairs in a hash can be defined in another way without using the =>
operator. Pairs can also be defined using adverb syntax. Adverb syntax is used throughout Raku to provide named data values, so it's not just useful for hashes. Pairs of the form "name" => data
can be written in adverb syntax as :name(data)
instead.
my %foo = "first" => 1, "second" => 2, "third" => 3;
my %bar = :first(1), :second(2), :third(3); # Same!
We're going to see many uses for adverbs throughout Raku, so it's important to learn about them now.
$_
The Default Variable
edit
Raku uses the special variable $_
as a special default variable. $_
receives values when no other variables are provided, and is used by methods if they start with a dot. $_
can be used explicitly by name or implicitly.
$_ = "Hello ";
.print; # Call method 'print' on $_
$_.print; # Same
print $_; # Same, but written as a sub;
given "world!" { # Different way of saying $_ = "world"
.print; # Same as print $_;
}
Default variables can be useful in a number of places, such as loops, where they can be used to clean up the code and make actions more explicit. We'll talk more about default variables as we go.