官术网_书友最值得收藏!

Using Unicode

The default assumption is that the source of a Perl 6 program uses UTF-8. It gives you the power of the whole spectrum of characters without worrying if it will work. In Perl 5, for example, you had to add special instructions in order to inform the interpreter that you are using non-ASCII characters in the source code. In Perl 6, this is much easier.

First, Unicode characters may be freely used in strings. For example, let's try some Greek and Chinese graphemes, as shown in the following lines of code:

say 'C = 2πr'; # Circumference of a circle say # 'Sun' and 'Moon' give 'bright'

The preceding two lines of code will print the corresponding strings as expected:

C = 2πr 

Alternatively, it is possible to refer to the Unicode codepoints by their names. For example, consider the following line of code:

say "Perl 6 is c[FLEXED BICEPS]";

The preceding line of code prints the following output with a muscle emoji:

Perl 6 is 

Using Unicode in Perl 6 is not limited to the strings, content only. Characters outside of ASCII may be used in the names of variables and functions. Let's return to the example of the add($x, $y) function from Chapter 1, What is Perl 6?, and rename it and its arguments using Greek letters:

sub Σ($α, $β) {
    return $α + $β;
}

say Σ(8, 9); # 17

Furthermore, some Unicode characters can be used to express simple actions instead of more conventional code. For example, here are the characters with rational fractions:

say ?;     # prints 0.5
say ? + ?; # 0.75

Non-ASCII Unicode digits are also allowed, but it is perhaps not a good idea to use them in a regular code without special needs:

say ??; # Prints 42

Superscripts such as 2 or 3 may be used instead of calling a function to calculate the power of a number, as shown in the following lines of code:

say 72; # 49
say 2?; # 128

Another example of using Unicode characters is the list of mathematical operations over sets, such as or .

In the next example, the Unicode versions of simple arithmetical operators are used instead of the regular * and / characters that are traditionally used:

say 10 × 4;  # 40
say 100 ÷ 4; # 25

In Perl 6 programs, you may often see a few other Unicode characters. Let's take a look at the most frequent of them.

The French quotes ? ? can be used in place of a pair of quotes to create lists. For example, in the following code, an @array gets the sequence of Fibonacci numbers. We've seen this example in Chapter 1, What is Perl 6?. Let's first update it to use the quoting syntax to creating arrays, as shown in the following lines of code:

my @fibonacci = <0 1 1 2 3 5 8 13>;
say @fibonacci[4]; # 3

We will see that kind of syntax in detail in Chapter 3, Working with Variables and Built-in Data Types; however, let's now see how to use the French quotes to enclose the list of values:

my @fibonacci = ?0 1 1 2 3 5 8 13?;
say @fibonacci[4]; # 3

For one-liners (short programs that are executed primarily in the -e command-line option), the Unicode quotes may be very helpful because they help to avoid the problem of nested single quotes. You can see how it works in the following example:

$ perl6 –e'say ‘Hello, World!’'

Instead of single quotes ('), a pair of Unicode quotes is used here ( and ).

When working with regular expressions and grammars (we will talk about them in Chapter 11, Regexes, and Chapter 12, Grammars), you will often see a pair of square angle brackets, which you may freely use in your code to quote strings, shown in the following code:

say ?Hello, World!?;

In Perl 6, the elements of the language, which can be expressed in Unicode characters, have equivalent forms written in ASCII. The ASCII characters are sometimes referred to as Texas characters. The following table lists the correspondence between the Unicode and the ASCII versions of the most common constructions:

 

Typing Unicode characters may be a separate task. Of course, you can always copy a character from Google or Wikipedia, but you will loose productivity in that case. Refer to the page Entering Unicode Characters in the Perl 6 online documentation available at docs.perl6.org/language/unicode_entry. It contains detailed instructions on how to enter the Unicode characters in different editors and IDEs.

主站蜘蛛池模板: 肇源县| 郁南县| 增城市| 藁城市| 阳曲县| 隆林| 伊宁市| 旬邑县| 沙洋县| 徐州市| 陆川县| 宜春市| 民丰县| 长宁县| 呼图壁县| 新宁县| 观塘区| 分宜县| 波密县| 全椒县| 昌吉市| 辰溪县| 会东县| 巩留县| 建水县| 盐津县| 大姚县| 麻城市| 韶山市| 左贡县| 聂拉木县| 得荣县| 嵩明县| 金昌市| 秦皇岛市| 奇台县| 仁怀市| 休宁县| 琼中| 南充市| 中超|