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

Multiline comments

Although one-line comments may be used to provide big chunks of documentation, having to place the # character in each line makes the comments themselves difficult to maintain. For example, if you modify the text of a comment, you may also want to reflow the whole paragraph so that all the lines are more or less the same length and the whole comment is more attractive visually.

In Perl 6, multi-line comments are supported. The syntax for a multi-line comment is the following. It starts with the #` sequence (the # character, same as in one-line comments, followed by a backtick symbol). Then, the part with the body of the comment goes in. It must be enclosed into a pair of brackets.

For example, curly braces may be used like this:

#`{This program in Perl 6
prints the 'Hello, World!' string}

say 'Hello, World!';

Here, the comment resides in two lines of the source code, but there is no need to mark every line of it with the # character.

Other pairs of embracing characters may be used. For example, parentheses or square brackets:

#`(A multi-line comment
placed between pair of parentheses)
#`[Another multi-line comment,
this time in square brackets]

Although the comments are intended to be read by humans in the first place, the compiler has to understand where the comment starts and ends. In the preceding example, the closing character of the comment body is defined by the corresponding character after the #` sequence.

If it is an opening brace ({), then the compiler scans the following text and looks for the closing counterpart, the } character in this example. This also means that you cannot use the closing brace in the text of the comment, because it will be treated as the end of comment.

One of the ways to have such a character in the comment is using a different pair of enclosing symbols. For example, if the whole comment is embedded in a pair of parentheses, then it is safe to use a closing square bracket in the body of that comment, as shown here:

#`(A multi-line comment
in parentheses and it contains the } character inside 
it)

Also, it will not be a problem if you use the balanced pairs of the same characters. For example, consider the following block of code:

#`(Function add(x, y) adds two numbers
and returns their sum)
sub add($x, $y) {
    return $x + $y;
}

Here, the comment is using the #`(…) pair of parentheses, but it contains another pair of parentheses inside: add(x, y). In this case, the program is correct and the comment ends where it should end according to the intention of the programmer.

The second way of allowing the same characters in the comment is to use a sequence of more than one character to mark the comment.

For example, a pair of double braces will work like this:

#`{{Two characters at the beginning
let us easily include the closing } brace, for 
example}}

Another good option is to use a combination of different characters. The closing sequence should mirror the opening one, as shown here:

#`([Another way of having  
a closing ] character inside the comment])

Finally, one-line comments may appear inside the multi-line comment. In this case, they will be just a part of it, as shown in the following example:

#`{If you want to print the value of the variable $x,
find the following line in the code:
# say $x
and uncomment it.}
主站蜘蛛池模板: 广饶县| 南汇区| 房产| 虞城县| 温宿县| 延吉市| 桐城市| 西乌珠穆沁旗| 贵港市| 定结县| 炉霍县| 雅安市| 平乐县| 双牌县| 瑞金市| 东乌珠穆沁旗| 阳谷县| 讷河市| 新晃| 绥德县| 云和县| 新乐市| 鹤岗市| 西宁市| 上饶县| 称多县| 全南县| 佛坪县| 绥芬河市| 广汉市| 大连市| 马鞍山市| 横山县| 葵青区| 讷河市| 福鼎市| 城步| 雷山县| 临洮县| 娱乐| 崇明县|