perl的内置函数scalar
scalar可以求数组的长度,但是,在scalar的说明里面并没有这一项。
Forces EXPR to be interpreted in scalar context and returns the value of EXPR.
中文含义:强制表达式EXPR在标量上下文进行解释。
假如,我定义了一个数组@arr=("one","two","three"),该数组在标量环境下返回自身的长度。
print @arr; #列表上下文,返回@arr元素 print scalar @arr #强制数组arr在标量上下文,这时,返回arr长度。
完整的例子:
[vagrant@bio ~]$ cat t.pl #!/usr/bin/env perl use v5.26; use warnings; my @arr=("hello","world"); say @arr; #默认列表上下文 say scalar @arr; #强制标量上下文 [vagrant@bio ~]$ perl t.pl helloworld 2
- 上一篇 »python 类,object的内置函数
- 下一篇 »Python常用内置函数的使用教程详解