Raku Programming/Variables and Data

Static and Dynamic Programming Languages
Raku 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:

The code above isn't entirely accurate since in C you can initialize a variable when you declare it:

However, the variable  cannot be used before it's declared. Once it's declared as the type, you can't use   to store other types of data like floating point numbers or data pointers, at least not without explicit coercion:

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:

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:. is used to declare a new variable for use. We'll talk more about  and its uses later.

Variables and Sigils
As 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
We've seen some uses of scalars above, here we're going to show a slightly more comprehensive list.

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
Arrays, 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:

Once we have an array we can extract the scalar data items out of them using index notation:

Arrays can also be multi-dimensional:

Arrays also don't have to just store scalars, they can store any other data items as well:

This can be the basis of some complex data structures, and we'll talk more about composing structures like this later.

Hashes
Hashes 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:

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:

Adverb Syntax
Pairs 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  can be written in adverb syntax as   instead.

We're going to see many uses for adverbs throughout Raku, so it's important to learn about them now.

The Default Variable
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.

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.