Wikibooksβ

Perl 6 Programming/Variables and Data

Contents

Perl 6 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:

unsignedshortintx;x=10;

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

unsignedshortintx=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:

In dynamic programming languages like Perl 6, variables can be automatically allocated when they are first used without having to be explicitly declared. Also, variables in Perl 6 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 Perl 6 interpreter. In Perl 6, most comments are marked with a # symbol, and continue until the end of the line. Perl 6 also has embedded comments and multi-line documentation, which we will talk about later.

We haven't been entirely honest about Perl 6 data. Perl 6 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 Perl 6's explicit type system later. For now, it's easier to think that data in Perl 6 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.

As we saw in the brief example above, Perl 6 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. Perl 6 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.

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:

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:

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:

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 Perl 6 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.

Perl 6 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;# Sameprint$_;# 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.