πŸ“˜ Built-in types in Perl 6

Perl 6 allows using typed variables. To tell the compiler that the variable is typed, you simply need to name the type while declaring the variable. Some of the types available in Perl 6 are obvious and do not need comments: Bool, Int, Str Array, Hash, Complex Some might require a small comment: Num, Pair, … Continue reading “πŸ“˜ Built-in types in Perl 6”

πŸ“˜ Variables in Perl 6: Twigils

In Perl 6, a variable name may be preceded by either a single-character sigil, such as $, @ or %, or with a double character sequence. In the latter case, this is called a twigil. The first character of it means the same thing that a bare sigil does, while the second one extends the … Continue reading “πŸ“˜ Variables in Perl 6: Twigils”

πŸ“˜ Variables in Perl 6: Sigils

Perl 6 uses sigils to mark variables. The sigils are partially compatible with the Perl 5 syntax. For instance, scalars, lists and hashes use, respectively, the $, @, and % sigils. my $scalar = 42; say $scalar; It’s not a surprise that the code prints 42. Consider the following fragment, which also gives a predictable … Continue reading “πŸ“˜ Variables in Perl 6: Sigils”

πŸ“˜ Bool in Perl 6

The usage of the Bool variables is straightforward although there are some details about which you might want to know. The Bool type is a built-in enumeration and provides two values: True and False (or, in a full form, Bool::True and Bool::False). It is permissible to increment or decrement the Boolean variables: my $b = … Continue reading “πŸ“˜ Bool in Perl 6”

πŸ“˜ Str type in Perl 6

Str is no doubt a string. In Perl 6, there are methods to manipulate strings. Again, you call them as methods on objects. my $str = “My string”; say $str.lc; # my string say $str.uc; # MY STRING say $str.index(‘t’); # 4 Let us now get the length of a string. The naΓ―ve attempt to … Continue reading “πŸ“˜ Str type in Perl 6”

πŸ“˜ Hash in Perl 6

Hashes provide a few methods with clear semantics, for instance: my %hash = Language => ‘Perl’, Version => 6; say %hash.elems;Β  # number of pairs in the hash say %hash.keys;Β Β  # the list of the keys say %hash.values; # the list of the values Here’s the output: Β 2 (Version Language) (6 Perl) It is possible … Continue reading “πŸ“˜ Hash in Perl 6”

πŸ“˜ Prefix operator + in Perl 6

+ is the unary plus operator, which casts its operand to the numerical context. The action is equivalent to the call of the Numeric method. my Str $price = ‘4’ ~ ‘2’; my Int $amount = +$price; say $amount;Β Β Β Β Β Β  Β # 42 say $price.Numeric; # 42 We will see one of the important use cases of … Continue reading “πŸ“˜ Prefix operator + in Perl 6”

πŸ“˜ Method postfix operator .+ in Perl 6

.+method makes an attempt to call all the methods with the given name on an object. This may be used, for example, when an instance is a part of the hierarchy of objects and its parent also has a method with the same name. More on the classes and class hierarchy in Chapter 4. class … Continue reading “πŸ“˜ Method postfix operator .+ in Perl 6”

πŸ“˜ Prefix operator ++ in Perl 6

++ is a prefix operator of increment. First, an increment is done, and then a new value is returned. my $x = 41; say ++$x; # 42 The increment operation is not limited to working only with numbers. It can also handle strings. my $a = ‘a’; say ++$a; # b A practical example is … Continue reading “πŸ“˜ Prefix operator ++ in Perl 6”

πŸ“˜ Prefix operator ^ in Perl 6

^ is a range-creating operator or the so-called upto operator. It creates a range (which is an object of the Range type) from 0 up to the given value (not including it). .print for ^5; # 01234 This code is equivalent to the following, where both ends of the range are explicitly specified: .print for … Continue reading “πŸ“˜ Prefix operator ^ in Perl 6”

πŸ“˜ Method postfix operator . in Perl 6

There are a few syntactical elements in Perl 6, which start with a dot. These operators might look like a postfix operator, but they all are the forms of the calling a method on an object. Unlike Perl 5, the dot operator does not do any string concatenation. .method calls a method on a variable. … Continue reading “πŸ“˜ Method postfix operator . in Perl 6”

πŸ“˜ Prefix operator ~ in Perl 6

~ casts an object to a string. Note that we are now talking about the prefix or a unary operator. If the tilde is used as an infix (see later in this chapter about what infixes are), it works as a string concatenating operator, but it still deals with strings. my Str $a = ~42; … Continue reading “πŸ“˜ Prefix operator ~ in Perl 6”