在 Perl 中,值可以未初始化但仍已定义吗?

发布于 2024-08-19 16:41:49 字数 395 浏览 5 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(2

许你一世情深 2024-08-26 16:41:49

在第一种情况下,匹配操作发生在标量上下文中。在第二种情况下,它发生在数组上下文中,几乎就像您编写的那样:

my @groups = $r->unparsed_uri =~ '/(logout.pl)?

如果 $r->unparsed_uri 匹配模式,但 $1 未定义,因为匹配的字符串以“/”结尾,那么@groups将是一个长度为1的数组,包含单个元素undef

把它们放在一起,就好像你在说:

die(undef);
; die @groups;

如果 $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:

my @groups = $r->unparsed_uri =~ '/(logout.pl)?

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 element undef.

Put it all together, it's as if you'd said:

die(undef);
; die @groups;

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 element undef.

Put it all together, it's as if you'd said:

鸠书 2024-08-26 16:41:49

您启用了警告吗?

鉴于

#!/usr/bin/perl -l

use strict; use warnings;

my $uri;

die(defined($uri =~ '/(logout.pl)?

我明白

Use of uninitialized value $uri in pattern match (m//) at E:\t.pl line 7.
1 at E:\t.pl line 7.

这解释了发生了什么。

$uri 未定义,因此您会在 m// 中使用它时收到警告。由于$uri未定义,因此匹配结果为false,但已定义。因此,define 返回 true,die 输出 1

));

我明白

这解释了发生了什么。

$uri 未定义,因此您会在 m// 中使用它时收到警告。由于$uri未定义,因此匹配结果为false,但已定义。因此,define 返回 true,die 输出 1

Do you have warnings enabled?

Given

#!/usr/bin/perl -l

use strict; use warnings;

my $uri;

die(defined($uri =~ '/(logout.pl)?

I get

Use of uninitialized value $uri in pattern match (m//) at E:\t.pl line 7.
1 at E:\t.pl line 7.

which explains what is going on.

$uri is not defined, so you get a warning for using that in m//. Because $uri is not defined, the result of the match is false but defined. Hence, defined returns true and die outputs 1.

));

I get

which explains what is going on.

$uri is not defined, so you get a warning for using that in m//. Because $uri is not defined, the result of the match is false but defined. Hence, defined returns true and die outputs 1.

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