简体   繁体   English

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

[英]Why is a list of undef not a read-only or constant value in Perl?

Consider the following programs in Perl. 考虑Perl中的以下程序。

use strict;
use warnings;

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

print scalar @foo;

This will die with an error message: 这将死于一个错误消息:

Modification of a read-only value attempted at ... 尝试修改只读值...

Using a constat will give a different error: 使用constat会产生不同的错误:

1 = shift @foo;

Can't modify constant item in scalar assignment at ... 无法修改标量赋值中的常量项...
Execution of ... aborted due to compilation errors. 执行...因编译错误而中止。

The same if we do this: 如果我们这样做:

(1) = shift @foo;

All of those make sense to me. 所有这些都对我有意义。 But putting undef in a list will work. 但是将undef放在列表中会起作用。

(undef) = shift @foo;

Now it prints 2 . 现在打印2

Of course this is common practice if you have a bunch of return values and only want specific ones, like here: 当然,如果您有一堆返回值并且只想要特定的值,这是常见的做法,例如:

my (undef, undef ,$mode, undef ,$uid, $gid, undef ,$size) = stat($filename);

The 9th line of code example in perldoc -f undef shows this, butthere is no explaination. perldoc -f undef中的第9行代码示例显示了这一点,但没有解释。

My question is, how is this handled internally by Perl? 我的问题是,这是如何由Perl内部处理的?

Internally, Perl has different operators for scalar assignment and list assignment, even though both of them are spelled = in the source code. 在内部,Perl具有用于标量赋值和列表赋值的不同运算符,即使它们在源代码中拼写为= And the list assignment operator has the special case for undef that you're asking about. 列表赋值运算符具有您要询问的undef的特殊情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM