11
在Perl中考虑以下程序。为什么undef中的列表不是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行显示这一点,butthere没有交代。
我的问题是,这是如何处理内部的Perl?
你能提供一个链接到源代码吗? – simbabque
到底是什么? Perl的源代码并不简单。你可以查看https://github.com/Perl/perl5/blob/blead/op.c并搜索“op_sassign”和“op_aassign”,如果有帮助的话。或者,也许更有用,通过做这样的事情来看编译的字节码:'perl -MO = Terse -E'(undef)= 1;'' –
对不起,我应该用不同的措辞。我的意思是,你能提供一些支持这种解释的东西吗?既然你没有链接到文档,说我认为你在其他地方找过它,最有可能的来源。 :) – simbabque