当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Perl qw用法及代码示例


描述

这个函数是指定很多小 single-quoted 字的快捷方式。例如,qw(foo bar baz) 等价于 ('foo', 'bar', 'baz')。一些程序员认为使用 qw 使 Perl 脚本更易于阅读。您实际上可以使用任何一组定界符,而不仅仅是括号。

您可以简单地使用 qw() 来准备一个数组,如下例所示。

用法

以下是此函数的简单语法 -

qw EXPR

返回值

此函数返回一个列表,该列表由 LIST 的元素组成,就好像它们是 single-quoted 一样。

示例

以下是显示其基本用法的示例代码 -

#!/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 qw Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。