珀尔 |返回()函数
Perl 中的 return()函数在子例程、块或执行函数的末尾返回值。根据所选上下文,返回值可能是标量、数组或散列。
Syntax: return Value
Returns:
a List in Scalar Context
注意:如果没有值传递给返回函数,则它在列表上下文中返回一个空列表,在标量上下文中返回 undef,在 void 上下文中返回任何内容。
示例 1:
#!/usr/bin/perl -w
# Subroutine for Multiplication
sub Mul($$)
{
my($a, $b ) = @_;
my $c = $a * $b;
# Return Value
return($a, $b, $c);
}
# Calling in Scalar context
$retval = Mul(25, 10);
print ("Return value is $retval\n" );
# Calling in list context
@retval = Mul(25, 10);
print ("Return value is @retval\n" );
输出:
Return value is 250
Return value is 25 10 250
示例 2:
#!/usr/bin/perl -w
# Subroutine for Subtraction
sub Sub($$)
{
my($a, $b ) = @_;
my $c = $a - $b;
# Return Value
return($a, $b, $c);
}
# Calling in Scalar context
$retval = Sub(25, 10);
print ("Return value is $retval\n" );
# Calling in list context
@retval = Sub(25, 10);
print ("Return value is @retval\n" );
输出:
Return value is 15
Return value is 25 10 15