Due to the mechanism of introspection, it is easily possible to tell the type of the data living in a variable (a variable in Perl 6 is often referred as a container). To do that, call the predefined WHAT method on a variable. Even if it is a bare scalar, Perl 6 treats it internally … Continue reading “π Variables in Perl 6: Introspection”
Category: Perl 6 at a Glance
π Int type in Perl 6
The Int type is intended to host integer variables of arbitrary size. For example, no digit is lost in the following assignment: my Int $x = Β Β Β 12389147319583948275874801735817503285431532; say $x; A special syntax exists for defining integers with an other-than-10 base: say :16<D0CF11E0> Also, it is allowable to use the underscore character to separate digits … Continue reading “π Int type in Perl 6”
π Prefix operators ! and not in Perl 6
Prefix operators are those that come in front of their operands. Obviously, prefix operators require only one operand. In some cases, the symbol of the operation can be used as an infix operator when it stands between two operands. ! is the Boolean negation operator. say !True;Β Β Β Β # False say !(1 == 2); # True … Continue reading “π Prefix operators ! and not in Perl 6”
π Frequently used special variables in Perl 6
The $_ variable is the one similar to that in Perl 5, which is the default variable containing the current context argument in some cases. Like any other variable, the $_ is an object in Perl 6, even in the simplest use cases. For example, the recent example .say for @*ARGS implicitly contains the $_.say … Continue reading “π Frequently used special variables in Perl 6”
π Hello, World! in Perl 6
The Perl 6 compiler can either read a programme from a file or from the content of the -e command line switch. The simplest βHello, World!β programme looks like this: say “Hello, Perl 6!”; Save it in a file and run: $ perl6 hello.pl Hello, Perl 6! Alternatively, you may use the -e option: $ … Continue reading “π Hello, World! in Perl 6”
π Array in Perl 6
The Array variables (i.e., all the variables starting with the @ sigil) are equipped with a couple of simple but rather useful methods. my @a = 1, 2, 3, 5, 7, 11; say @a.Int; # array length say @a.Str; # space-separated values If you print an array, you get its value as a space-separated list … Continue reading “π Array in Perl 6”
π Prefix operators ? and so in Perl 6
? is a unary operator casting the context to a Boolean one by calling the Bool method on an object. say ?42; # True The second form, so, is a unary operator with lower precedence. say so 42;Β Β # True say so True; # True say so 0.0;Β # False
π Prefix operator temp in Perl 6
temp creates a temporary variable and restores its value at the end of the scope (like it does the local built-in operator in Perl 5). my $x = ‘x’; { Β Β Β temp $x = ‘y’; Β Β Β say $x; # y } say $x;Β Β Β Β # x Compare it with the following operator, let.
π 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”
π 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 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”
π Numeric operators ?|, ?&, and ?^ 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.
π Prefix operator – in Perl 6
– 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
π 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”