gpt4 book ai didi

perl - 为什么 undef 列表在 Perl 中不是只读或常量值?

转载 作者:行者123 更新时间:2023-12-04 02:09:36 25 4
gpt4 key购买 nike

考虑以下 Perl 程序。

use strict;
use warnings;

my @foo = qw(a b c);
undef = shift @foo;

print scalar @foo;

这将随着错误消息而消失:

Modification of a read-only value attempted at ...



使用 constat 会产生不同的错误:
1 = shift @foo;

Can't modify constant item in scalar assignment at ...
Execution of ... aborted due to compilation errors.



同样,如果我们这样做:
(1) = shift @foo;

所有这些对我来说都很有意义。但是把 undef在列表中将起作用。
(undef) = shift @foo;

现在它打印 2 .

当然,如果您有一堆返回值并且只想要特定的返回值,这是常见的做法,例如:
my (undef, undef ,$mode, undef ,$uid, $gid, undef ,$size) = stat($filename);

perldoc -f undef中的第9行代码示例显示了这一点,但没有解释。

我的问题是,这是如何由 Perl 内部处理的?

最佳答案

在内部,Perl 有不同的用于标量赋值和列表赋值的运算符,即使它们都拼写为 =在源代码中。列表赋值运算符有 undef 的特殊情况。你要问的。

关于perl - 为什么 undef 列表在 Perl 中不是只读或常量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32436023/

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