在 Perl 中,值可以未初始化但仍已定义吗?
在 win32 上运行 ActiveState Perl 5.10.1。
这段代码是怎么回事:
die(defined($r->unparsed_uri =~ '/(logout.pl)?$'));
...dies with 1
,而将同一行更改为:
die($r->unparsed_uri =~ '/(logout.pl)?$');
...dies with Use of uninitialized value in die
?
它是如何已定义
但未初始化
的?我认为未初始化意味着未定义。
Running ActiveState Perl 5.10.1 on win32.
How is it that this code:
die(defined($r->unparsed_uri =~ '/(logout.pl)?
...dies with 1
, whereas changing the same line to say this:
die($r->unparsed_uri =~ '/(logout.pl)?
...dies with Use of uninitialized value in die
?
How is it defined
yet uninitialized
? I thought uninitialized meant undefined.
));
...dies with 1
, whereas changing the same line to say this:
...dies with Use of uninitialized value in die
?
How is it defined
yet uninitialized
? I thought uninitialized meant undefined.
);
...dies with Use of uninitialized value in die
?
How is it defined
yet uninitialized
? I thought uninitialized meant undefined.
...dies with 1
, whereas changing the same line to say this:
...dies with Use of uninitialized value in die
?
How is it defined
yet uninitialized
? I thought uninitialized meant undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在第一种情况下,匹配操作发生在标量上下文中。在第二种情况下,它发生在数组上下文中,几乎就像您编写的那样:
如果
$r->unparsed_uri
匹配模式,但$1
未定义,因为匹配的字符串以“/”结尾,那么@groups将是一个长度为1的数组,包含单个元素undef
。把它们放在一起,就好像你在说:
In the first case, the matching operation is taking place in scalar context. In the second case, it's taking place in array context, almost as if you had written:
If
$r->unparsed_uri
matches the pattern, but$1
is undefined because the matched string ended with "/", then @groups will be an array of length 1, containing the single elementundef
.Put it all together, it's as if you'd said:
您启用了警告吗?
鉴于
我明白
这解释了发生了什么。
$uri
未定义,因此您会在m//
中使用它时收到警告。由于$uri
未定义,因此匹配结果为false,但已定义。因此,define
返回 true,die
输出1
。Do you have warnings enabled?
Given
I get
which explains what is going on.
$uri
is not defined, so you get a warning for using that inm//
. Because$uri
is not defined, the result of the match is false but defined. Hence,defined
returns true anddie
outputs1
.