珀尔 |标量上下文敏感性
介绍:
在 Perl 中,函数调用、术语和语句具有依赖于其上下文的不一致的解释。 Perl 中有两个关键的上下文,即列表上下文和标量上下文。在列表上下文中,Perl 给出元素列表。但在标量上下文中,它返回数组中元素的数量。当运算符在标量上运行时,它被称为标量上下文。
笔记:
- 每当您将任何内容分配给标量变量时,它总是会提供标量上下文。
- 在此上下文中,假定是获得单个值。
- 如果分配给标量变量的数组将返回其大小。
标量上下文可以使用标量变量、数字运算符等生成。
- 分配给标量变量:
例子:$x = @z;
$x = localtime();
$x = Scalar;
在这里,localtime() 以人类可读的格式显示时间,而在 List Context 中,此函数显示时间的数字描述。
- 分配给数组的单个元素:
例子:$a[2] = Scalar;
数组的每个元素都是单独的标量。因此,分配给它们会生成标量上下文。
- 创建标量上下文的数值运算符:
例子:3 + Scalar;
Scalar + 3;
数值运算符可以在其两侧生成标量上下文。
- 连接创建标量上下文:
例子:"GFG" . Scalar;
Scalar . "GFG"
从上面的例子可以看出,Concatenation 可以在自身的两端生成 Scalar Context。
例子:
#!/usr/bin/perl
# Perl program of creating Scalar Context
# array of elements
my @CS = ('geeks', 'for', 'geeks', 'articles');
# Assignment to a Scalar variable
my $x = @CS;
# Assignment of a function
# to a Scalar variable
# Note: Time displayed here
# will be the GMT
my $y = localtime();
# Numerical operator creating
# Scalar Context
my $z = 3 + @CS;
# Displays number of elements
# in an Array
print "$x\n";
# Displays time stored in array
# in human readable format
print "$y\n";
# Displays sum of a number
# and Scalar
print "$z\n";
# Concatenation creating
# Scalar Context
print "The number of elements are: " . @CS
4
Wed Mar 27 07:01:56 2019
7
The number of elements are: 4
当 Perl 假定一个列表时,必须要求强制标量上下文。因此,在这种情况下,您可以使用生成标量上下文的scalar()函数,因为该函数通知 Perl 为其参数赋予标量上下文。
例子:
#!/usr/bin/perl
# Perl program of Forcing Scalar Context
# array of elements
my @x = ('geeks', 'for', 'geeks');
# Forcing Scalar context to display
# number of elements in an Array
print scalar @x;
print "\n";
# Displaying time in human readable
# format by forcing Scalar Context
print scalar localtime();
3
Sun Mar 17 06:12:53 2019
为了使用数组触发标量上下文,需要将数组分配给标量变量。
例子:
#!/usr/bin/perl
# Perl program of Arrays in Scalar Context
# array of elements
my @x = ('geeks', 'for', 'geeks');
# Assignment of an Array to
# a Scalar variable
my $y = @x;
# Displays number of elements in
# an Array
print $y;
3
当 if 语句的条件部分假定单个值时,那就是标量上下文。在下面的程序中,if 语句包含数组,在标量上下文中,数组返回其中元素的数量。因此,如果数组为空,那么它将返回 0,因此如果作为标量上下文传递给它的数组为空,则 if 语句将不会执行。
方案一:
#!/usr/bin/perl
# Program of if-statement in Scalar Context
use strict;
use warnings;
use 5.010;
# Array with no elements
my @w = ();
# Statement within 'if' will be executed
# only if the array is not empty
if (@w)
{
print "Geeks";
}
No Output
在这里,没有打印任何内容,因为声明的 Array 是空的。因此,代码不会显示 if 语句的内容。
方案二:
#!/usr/bin/perl
# Program of if-statement in Scalar Context
use strict;
use warnings;
use 5.010;
# An Array of elements
my @w = ('G', 'f', 'G');
# Statement within 'if' will be executed
# only if the array is not empty
if (@w)
{
print "There are some elements in the Array";
}
There are some elements in the Array
在这里,上述数组不为空,因此打印了 if 语句的内容。
为了在标量上下文中放置 readline运算符(即
例子:
#!/usr/bin/perl
# Program to Read input from user
use strict;
use 5.010;
# Asking the user to provide input
print "Enter your name:\n";
# Getting input from user
my $y = ;
# Printing the required output
print "My name is $y\n";
输出:
上面的程序使用