gpt4 book ai didi

perl - 方法调用不提供标量上下文......似乎很奇怪

转载 作者:行者123 更新时间:2023-12-04 05:23:49 25 4
gpt4 key购买 nike

此行为不是 Math::BigInt 特定的,但以下代码在最后一行中断。

use strict;
use warnings;
use Math::BigInt;

my $a = Math::BigInt->bone;
my $b = Math::BigInt->bone;

print ($a+$b)->bfac;

但是,此代码工作正常:
use strict;
use warnings;
use Math::BigInt;

my $a = Math::BigInt->bone;
my $b = Math::BigInt->bone;

print scalar($a+$b)->bfac;

我的问题是……为什么标量上下文不自动强加在“->”的左参数上? AFAIK,“->”仅适用于标量和(异常(exception))类型团。

最佳答案

你还需要一组括号,

print (($a+$b)->bfac);

因为您的代码被解释为,
(print ($a+$b))->bfac;

和警告也给了你 print (...) interpreted as function ..

关于perl - 方法调用不提供标量上下文......似乎很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26010370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com