跳轉到內容

Raku 程式設計/註釋和 POD

來自華夏公益教科書

現在我們已經涵蓋了 Raku 程式設計的大部分基礎知識。我們並沒有完全涵蓋這門語言。但是,我們已經看到了普通程式設計任務所需的各種基本工具。還有更多內容需要學習,許多高階工具和功能可以用來簡化常見任務,以及使困難的任務成為可能。我們將在稍後介紹一些這些更高階的功能,但在本章中,我們想透過討論註釋和文件來結束“基礎”部分。

我們之前提到過,註釋是在原始碼中供程式設計師閱讀的筆記,Raku 直譯器會忽略這些筆記。Raku 中最常見的註釋形式是單行註釋,以單個井號 # 開頭,一直延伸到行尾。

# Calculate factorial of a number using recursion
sub factorial (Int $n) {
    return 1 if $n == 0;            # This is the base case
    return $n * factorial($n - 1);  # This is the recursive call
}

當執行上述程式碼時,所有以單個井號 # 為字首的文字將被 Raku 直譯器忽略。

多行註釋

[編輯 | 編輯原始碼]

雖然 Perl 不提供多行註釋,但 Raku 提供。為了在 Raku 中建立多行註釋,註釋必須以單個井號開頭,後面跟著一個反引號,然後是一些開啟的括號字元,最後以匹配的關閉括號字元結尾。

sub factorial(Int $n) {
    #`( This function returns the factorial of a given parameter
      which must be an integer. This is an example of a recursive
      function where there is a base case to be reached through
      recursive calls.
      )
    
    return 1 if $n == 0;            # This is the base case
    return $n * factorial($n - 1);  # This is the recursive call
}

此外,註釋的內容也可以嵌入在內聯中。

sub add(Int $a, Int $b) #`( two (integer) arguments must be passed! ) {
    return $a + $b;
}

POD 文件

[編輯 | 編輯原始碼]
華夏公益教科書