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”
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 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”
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”
+ 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”
+^ is a bitwise negation operator with two’s complement. my $x = 10; my $y = +^$x; say $y; # -11 (but not -10) Compare this operator with the following one.
.+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”
?|, ?&, and ?^ cast the operands to the Boolean type (thus the ? in the operator name) and do the logical operations of OR, AND, and XOR.
– is a unary minus, which changes the sign of its operand. Because this operator silently calls the Numeric method, it can also cast the context, as it does the unary plus operator. my Str $price = ‘4’ ~ ‘2’; say -$price; # -42
++ 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”