强制成为标量

发布于 2024-12-11 13:49:32 字数 211 浏览 0 评论 0原文

#!/usr/bin/perl
use Modern::Perl;
while (<>)
{ chomp;
say reverse;  
}

上面的代码不起作用,但是当我将倒数第二行更改为 say scalar reverse; 时,它就可以正常工作。为什么我需要显式地强制它成为标量? Perl 不能 DWIM 吗?

#!/usr/bin/perl
use Modern::Perl;
while (<>)
{ chomp;
say reverse;  
}

The above code doesn't work but when I change 2nd last line to say scalar reverse; then it works fine. Why do I need to force it to be a scalar explicitly? Can't Perl DWIM?

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

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

发布评论

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

评论(2

佼人 2024-12-18 13:49:32

如果我对文档的理解正确的话,reverse 通常在列表上运行。在不带参数的列表上下文中,它返回一个空列表,并且默认情况下不会将其分配到任何地方。在您的示例中,假设输出未更改的 $_;

强制 reverse 进入标量上下文会更改其行为并使其反转字符串,并默认使用 $_。由于 say 可用于打印列表和标量,因此它不会强制将其参数放入标量上下文中。

Perl 可能会执行 DWIM,只是针对给定的“I”值。

详细了解反向操作在以下情况下的作用:

#!/usr/bin/env perl
use strict;
use v5.12;

my $inscalar = "foo bar";

# `reverse` is in list context, $in is coerced to a single-element list
my @outlist = reverse $inscalar;

# `reverse` is in scalar context, so it reverses strings
my $outscalar = reverse $inscalar;

say join(" | ", @outlist); # prints "foo bar"
say $outscalar; # prints "rab oof"

# the "normal" behaviour of `reverse`
my @inlist = qw(foo bar);
@outlist = reverse @inlist;
say join(" | ", @outlist); # prints "bar | foo"

If I understand the documentation right, reverse normally operates on lists. In a list context used without arguments, it returns an empty list and by default doesn't assign it anywhere. In your example, say outputs the unchanged $_;

Forcing reverse into scalar context changes its behaviour and makes it reverse character strings, and use $_ by default. Because say can be used to print lists as well as scalars, it doesn't force its arguments into scalar context.

Perl probably does DWIM, just for given values of "I".

A breakdown of what reverse does when:

#!/usr/bin/env perl
use strict;
use v5.12;

my $inscalar = "foo bar";

# `reverse` is in list context, $in is coerced to a single-element list
my @outlist = reverse $inscalar;

# `reverse` is in scalar context, so it reverses strings
my $outscalar = reverse $inscalar;

say join(" | ", @outlist); # prints "foo bar"
say $outscalar; # prints "rab oof"

# the "normal" behaviour of `reverse`
my @inlist = qw(foo bar);
@outlist = reverse @inlist;
say join(" | ", @outlist); # prints "bar | foo"
心如荒岛 2024-12-18 13:49:32

为什么我需要显式地强制它成为标量?

正如记录的reverse反转了元素的顺序在列表上下文中使用时的参数列表。例如,反转 'a'、'b'、'c' 返回 'c'、'b'、'a'

Perl 不能 DWIM 吗?

是的,它可以执行您想要的操作(不存在参数表达式时的标量行为)。

不过,这可能会令人困惑。我想不出任何其他运算符会因为缺少参数表达式(而不是仅仅缺少参数)而改变行为。

Why do I need to force it to be a scalar explicitly?

As documented, reverse reverses the order of the elements of the argument list when used in list context. For example, reverse 'a', 'b', 'c' returns 'c', 'b', 'a'.

Can't Perl DWIM?

Yes, it could do what you want (scalar behaviour when no argument expression is present).

It could be confusing, though. I can't think of any other operator that changes behaviour based on a lack of an argument expression (as opposed to just lack of arguments).

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