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