如何解决“在空上下文中无用使用变量”的情况?

发布于 2024-08-23 10:42:19 字数 207 浏览 6 评论 0 原文

如何解决“在空上下文中无用使用变量”的情况?

例如:

  my $err = $soap_response->code, " ", $soap_response->string, "\n";
  return $err;

我收到诸如“在 void 上下文中无用使用变量”之类的警告?为什么?我该如何解决?

How can I resolve this case of "Useless use of a variable in a void context"?

For example:

  my $err = $soap_response->code, " ", $soap_response->string, "\n";
  return $err;

I get warnings like "Useless use of a variable in a void context"? Why? How can I resolve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咿呀咿呀哟 2024-08-30 10:42:20

如果您想连接参数,请使用 "." 运算符或 join

my $err = $soap_response->code. " ". $soap_response->string. "\n";
my $err = join '', $soap_response->code, " ", $soap_response->string, "\n";

接下来是 Perl 向您发出警告的原因。

您正在分配给标量变量 $err,并且分配的右侧在标量上下文中进行计算。

二进制“,”是逗号运算符。在标量上下文中,它在 void 上下文中计算其左参数,丢弃该值,然后在标量上下文中计算其右参数并返回该值。

评估变量或常量并丢弃该值是没有用的。 Perl 会对此发出警告。

仅供参考:您的代码可能存在另一个问题:

my $err = $soap_response->code, " ", $soap_response->string, "\n";

分配具有更高的优先级,因此:

(my $err = $soap_response->code), " ", $soap_response->string, "\n";

请参阅 Perl 运算符和优先级逗号运算符 了解更多信息。

In case you want to concatenate the arguments, use the "." operator or join:

my $err = $soap_response->code. " ". $soap_response->string. "\n";
my $err = join '', $soap_response->code, " ", $soap_response->string, "\n";

Next is why Perl gives you warnings.

You're assigning to a scalar variable $err, and the right-hand side of the assignment is evaluated in scalar context.

Binary "," is the comma operator. In scalar context it evaluates its left argument in void context, throws that value away, then evaluates its right argument in scalar context and returns that value.

Evaluating a variable or a constant and throwing that value away is useless. And perl warns you about this.

FYI: Another possible issue with your code:

my $err = $soap_response->code, " ", $soap_response->string, "\n";

The assignment has higher precedence so that is:

(my $err = $soap_response->code), " ", $soap_response->string, "\n";

See Perl operators and precedence and the Comma operator for more information.

撧情箌佬 2024-08-30 10:42:20

我猜您想连接字符串片段以形成整个错误消息,因此您必须使用点而不是逗号:

my $err = $soap_response->code. " ". $soap_response->string. "\n";

I guess you wanted to concatenate the string pieces to form the entire error message, so you'll have to use the dot instead of comma:

my $err = $soap_response->code. " ". $soap_response->string. "\n";
木緿 2024-08-30 10:42:20
my $err = join(' ', $soap_response->code, $soap_response->string) . "\n";

或者,更好的 IMO:

return sprintf "%s %s\n", $soap_response->code, $soap_response->string;

请参阅 perldoc -f joinperldoc -f sprintf perldoc perlop

关于警告,请参阅 perldoc perlop关于逗号运算符的注释

my $err = join(' ', $soap_response->code, $soap_response->string) . "\n";

or, better IMO:

return sprintf "%s %s\n", $soap_response->code, $soap_response->string;

See perldoc -f join and perldoc -f sprintf perldoc perlop.

Regarding the warning, see perldoc perlop and this note on the comma operator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文