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

  • Perl 6 Deep Dive
  • Andrew Shitov
  • 810字
  • 2021-07-03 00:05:45

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.

主站蜘蛛池模板: 石狮市| 彩票| 黔东| 嘉义市| 灌南县| 定安县| 怀安县| 建阳市| 南川市| 达尔| 闽清县| 聂拉木县| 嘉善县| 松滋市| 丰顺县| 富源县| 都江堰市| 宝鸡市| 岳阳县| 万安县| 舒兰市| 博野县| 云龙县| 湖南省| 北票市| 出国| 噶尔县| 陆丰市| 偏关县| 陈巴尔虎旗| 霍林郭勒市| 嘉峪关市| 太保市| 达州市| 黄骅市| 昭平县| 通化市| 深圳市| 乐东| 远安县| 县级市|