Perl 抱怨正则表达式中的未初始化值

发布于 2024-12-09 17:47:47 字数 659 浏览 1 评论 0原文

我有一个函数如下:

sub getMinFromParam {
    my ($param) = @_;

    print "PPPP = $param\n";

    my $min;
    if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/) {
    print "IN HERE\n";
        $min = $1;
    }
    elsif ($min =~ /((\w+),)+/) {
        my @tmp = split (/\s*,\s*/, $param);
        if ($tmp[0] =~ /\[(\w+),\s?(\w+)\]/) {
            $min = $1;
        }
        else {
            $min = $tmp[0];
        }
    }
    return ($min);
}

当 $param 是一个字符串时,例如: 120u, 421u, 53, 19, 41u, 53, 我收到错误:在模式匹配 (m//) 中使用未初始化的值在这一行:

if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/) {

为什么在定义 $param 时抱怨未初始化的值?

I have a function below:

sub getMinFromParam {
    my ($param) = @_;

    print "PPPP = $param\n";

    my $min;
    if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/) {
    print "IN HERE\n";
        $min = $1;
    }
    elsif ($min =~ /((\w+),)+/) {
        my @tmp = split (/\s*,\s*/, $param);
        if ($tmp[0] =~ /\[(\w+),\s?(\w+)\]/) {
            $min = $1;
        }
        else {
            $min = $tmp[0];
        }
    }
    return ($min);
}

When $param is a string like: 120u, 421u, 53, 19, 41u, 53,
I get the error: Use of uninitialized value in pattern match (m//) at this line:

if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/) {

Why is it complaining about uninitialized value when $param is defined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷了相思 2024-12-16 17:47:47

它不是。它抱怨这一行:

elsif ($min =~ /((\w+),)+/) {

至少这里有测试数据 120u, 421u, 53, 19, 41u, 53

这是预期的,因为 $min 仅在以下情况下设置: code>$param 匹配,但 elsif 仅在 $param 不匹配时运行。

该行可能应该是 elsif ($param =~ /((\w+),)+/) {,在这种情况下我不会收到警告。

It's not. It's complaining about this line:

elsif ($min =~ /((\w+),)+/) {

At least here with the test data 120u, 421u, 53, 19, 41u, 53

Which is expected, since $min is only set if $param matches, but the elsif only runs if $param doesn't match.

That line is probably supposed to be elsif ($param =~ /((\w+),)+/) {, in which case I don't get a warning.

戏剧牡丹亭 2024-12-16 17:47:47

实际上,$min 是未定义的,当您使用 strictwarnings 时,它会被报告。这修复了它:

use strict;
use warnings;

sub getMinFromParam
{
    my ($param) = @_;

    print "PPPP = $param\n";

    my $min;
    if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/)
    {
        print "IN HERE\n";
        $min = $1;
    }
    elsif (defined $min && $min =~ /((\w+),)+/)
    {
        my @tmp = split (/\s*,\s*/, $param);
        if ($tmp[0] =~ /\[(\w+),\s?(\w+)\]/)
        {
            $min = $1;
        }
        else
        {
            $min = $tmp[0];
        }
    }
    return ($min);
}

getMinFromParam "120u, 421u, 53, 19, 41u, 53"

It is actually $min that is undefined, and it gets reported when you use strict and warnings. This fixes it:

use strict;
use warnings;

sub getMinFromParam
{
    my ($param) = @_;

    print "PPPP = $param\n";

    my $min;
    if ($param =~ /\s*\[(\S+),\s*(\S+)\]\s*/)
    {
        print "IN HERE\n";
        $min = $1;
    }
    elsif (defined $min && $min =~ /((\w+),)+/)
    {
        my @tmp = split (/\s*,\s*/, $param);
        if ($tmp[0] =~ /\[(\w+),\s?(\w+)\]/)
        {
            $min = $1;
        }
        else
        {
            $min = $tmp[0];
        }
    }
    return ($min);
}

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