πŸ“˜ String concatenation operator ~ in Perl 6

~ does the string concatenation. The dot in Perl 6 is now used for dealing with methods; thus, a new operator for the string concatenation was required. The tilde was a good candidate because it is also used in other string-related operators in Perl 6. say “a” ~ “b”; # ab If necessary, the operator … Continue reading “πŸ“˜ String concatenation operator ~ in Perl 6”

πŸ“˜ String comparison operator leg in Perl 6

leg tells if is the two strings are equal or the left operand is less or greater than the second one. Its behaviour is similar to what <=> does for numbers or what the cmp built-in operator does in Perl 5. Like the cmp in Perl 6, the leg operator returns a value of the … Continue reading “πŸ“˜ String comparison operator leg in Perl 6”

πŸ“˜ Universal comparison operators before and after in Perl 6

before and after are the comparison operators, which work with operands of different types. It returns a Boolean value of either True or False depending on which operand was considered to be ordered earlier or later. Type coercion is similar to how it happens with the cmp operator.Β  Remember that depending on the data types … Continue reading “πŸ“˜ Universal comparison operators before and after in Perl 6”

πŸ“˜ Smartmatch operator ~~ in Perl 6

~~ is the smartmatch operator. It compares the objects and tries to work correctly with the operands of any type (that is why the operator is called smart). say 42 ~~ 42.0; # True say 42 ~~ “42”; # True The result of the smartmatching depends on the operand order. Consider the following: say “42.0” … Continue reading “πŸ“˜ Smartmatch operator ~~ in Perl 6”

πŸ“˜ Junction operators |, &, and ^ in Perl 6

|, &, and ^ create the so-called junctions (formerly known in Perl 6 as quantum superpositions). These objects can be used where a scalar is used but behave differently; unlike the scalars, the junctions have multiple values at the same moment in time. The |, &, and ^ operators create, respectively, the junctions of the … Continue reading “πŸ“˜ Junction operators |, &, and ^ in Perl 6”

πŸ“˜ Invocant call operator : in Perl 6

: marks the left side of it as an invocant to call a method on, when a method of an object is used. It is easier to understand how it works in the following example. class C { Β Β Β  method meth($x) { Β Β Β Β Β Β Β  say “meth($x)”; Β Β Β  } } my $o = C.new; meth($o: 42); # … Continue reading “πŸ“˜ Invocant call operator : in Perl 6”

πŸ“˜ Default values in Perl 6 subs

Perl 6 also allows specifying the default values of the sub’s arguments. Syntactically, this looks like an assignment. sub i-live-in(Str $city = “Moscow”) {Β Β  Β Β Β  say “I live in $city.”;Β Β Β Β Β Β Β Β Β Β Β Β  } i-live-in(‘Saint Petersburg’); i-live-in(); # The default city It is also possible to pass values that are not known at the compile phase. When the … Continue reading “πŸ“˜ Default values in Perl 6 subs”

πŸ“˜ Universal comparison operator cmp in Perl 6

There are a few operators, which can compare both strings and numbers, or even compound objects like pairs. cmp compares two objects and returns a value of the Order type, either Less, or Same, or More. say 2 cmp 2;Β Β  # Same say 2 cmp 2.0; # Same say 1 cmp 2;Β Β  # Less say … Continue reading “πŸ“˜ Universal comparison operator cmp in Perl 6”

πŸ“˜ Universal comparison operator eqv in Perl 6

eqv is an operator that tests the two operands for equivalence. It returns the True value if the operands are of the same type and contain the same values. my $x = 3; my $y = 3; say $x eqv $y; # True An example with a bit more complex data structures: my @a = … Continue reading “πŸ“˜ Universal comparison operator eqv in Perl 6”

πŸ“˜ Cross-product operator X in Perl 6

X is the cross product operator, which converts the two given lists to a third one containing all the possible combinations of the elements from the original lists. @c = @a X @b; This is the same as the following sequence: @c = ((@a[0], @b[0]), (@a[0], @b[1]), (@a[0], @b[2]), … (@a[N], @b[0]), (@a[N], @b[1]), … … Continue reading “πŸ“˜ Cross-product operator X in Perl 6”

πŸ“˜ Subroutines, or subs in Perl 6

For a sub, which takes no arguments, its definition and the call are very straightforward and easy. sub call-me { Β Β Β  say “I’m called” } call-me; The syntax for declaring a sub’s parameters is similar to what other languages (including Perl 5.20 and higher) provide. sub cube($x) { Β Β Β  return $x ** 3; } say … Continue reading “πŸ“˜ Subroutines, or subs in Perl 6”