Perl qw 函式



說明

此函式是一種快速指定大量小單引號詞語的方法。例如,qw(foo bar baz) 等同於 ('foo', 'bar', 'baz'). 有些程式設計師會認為使用 qw 會使 Perl 指令碼更易讀。您實際上可以使用任意一組分隔符,而不侷限於圓括號。

您可以使用 qw() 來準備一個數組,如下例所示。

語法

以下為該函式的簡單語法 −

qw EXPR

返回值

此函式會返回包含一份清單,清單中由 LIST 的元素組成(已對其進行求值,如同它們被單引號引起來的一樣)。

示例

以下示例程式碼展示了它的基本用法 −

#!/usr/bin/perl -w

@array = qw(This is a list of words without interpolation);

foreach $key (@array) {
   print"Key is $key\n";
}

當執行上述程式碼時,它會產生以下結果 −

Key is This
Key is is
Key is a
Key is list
Key is of
Key is words
Key is without
Key is interpolation
perl_function_references.htm
廣告
© . All rights reserved.