当我在constant.pm 中使用哈希引用时,为什么 Perl 会抱怨?

发布于 2024-07-13 07:19:11 字数 1270 浏览 2 评论 0原文

我有 ActiveState 提供的为 MSWin32-x86-多线程二进制版本 638 构建的 perl v5.6.1。

我正在编写一个 Perl 脚本,其中声明了稍后用于比较目的的常量。 由于某种原因,我收到一个错误,指出 Constant name 行中的某些内容在脚本的第 31 行(下面代码中 use Constant 子句后面的行)具有无效字符。 我检查发现常量名称中的“_”(下划线)是合法字符。 我还尝试将“0.00”更改为“0”,看看这是否是原因,但我得到了同样的错误。 我不确定我做错了什么。 有人知道为什么编译器不喜欢这样吗?

谢谢!

代码如下:

use constant {
MIN_NET_DLR => 0.00,
MAX_NET_DLR => 99.99,
MIN_SUM_DLR => 0.00,
MAX_SUM_DLR => 999.99,
MIN_UNITS => 0,
MAX_UNITS => 99,
MIN_SUM_UNITS => 0,
MAX_SUM_UNITS => 999,
PCT_THRES_AO => 1,
PCT_THRES_TRANS_CUST_BI => 20,
PCT_THRES_CUST => 3,
};

问题:

问题是 perl 5.6.1 提供的常量版本不支持哈希引用。

解决方案:

使用常量的常规声明。 因此,声明将如下所示:

use constant MIN_NET_DLR => 0.00;
use constant MAX_NET_DLR => 99.99;
use constant MIN_SUM_DLR => 0.00;
use constant MAX_SUM_DLR => 999.99;
use constant MIN_UNITS => 0;
use constant MAX_UNITS => 99;
use constant MIN_SUM_UNITS => 0;
use constant MAX_SUM_UNITS => 999;
use constant PCT_THRES_AO =>    1;
use constant PCT_THRES_TRANS_CUST_BI => 20;
use constant PCT_THRES_CUST => 3;

感谢@leon 提供的解决方案以及其他提出意见的人。

更新:另一种(更优雅的)解决方案是将 Perl 版本更新为支持声明常量时的哈希引用的版本。

I have perl, v5.6.1 built for MSWin32-x86-multi-thread Binary build 638 provided by ActiveState.

I am working on a Perl script where I have declared constants that are used later for comparison purposes. For some reason, I am getting an error that states something along the line of Constant name has invalid characters at script's line 31 (The line right after the use constant clause in the code below). I checked and found out that '_' (underscores) in Constant name is a legit character. I also tried to change '0.00' to just '0' to see if that was the cause but I got the same error. I am not sure what I am doing wrong. Anyone know why the compiler does not like this?

Thanks!

Here is the Code:

use constant {
MIN_NET_DLR => 0.00,
MAX_NET_DLR => 99.99,
MIN_SUM_DLR => 0.00,
MAX_SUM_DLR => 999.99,
MIN_UNITS => 0,
MAX_UNITS => 99,
MIN_SUM_UNITS => 0,
MAX_SUM_UNITS => 999,
PCT_THRES_AO => 1,
PCT_THRES_TRANS_CUST_BI => 20,
PCT_THRES_CUST => 3,
};

PROBLEM:

The problem is that the version of constant provided by perl 5.6.1 does not support hash reference.

SOLUTION:

Use the regular declaration for constants. Hence, the declaration will look as follows:

use constant MIN_NET_DLR => 0.00;
use constant MAX_NET_DLR => 99.99;
use constant MIN_SUM_DLR => 0.00;
use constant MAX_SUM_DLR => 999.99;
use constant MIN_UNITS => 0;
use constant MAX_UNITS => 99;
use constant MIN_SUM_UNITS => 0;
use constant MAX_SUM_UNITS => 999;
use constant PCT_THRES_AO =>    1;
use constant PCT_THRES_TRANS_CUST_BI => 20;
use constant PCT_THRES_CUST => 3;

Thanks @leon for the solution as well as others who chimed in with their inputs.

UPDATE: Another (more elegant) solution is to update your Perl version to the one that supports hash reference in declaring constants.

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

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

发布评论

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

评论(7

痴者 2024-07-20 07:19:11

问题是您正在使用哈希引用。 perl 5.6.1 提供的 constant 版本不支持这一点。 您要么必须删除大括号,要么升级您的常量

The problem is that you are using a hash reference. The version of constant provided by perl 5.6.1 does not support that. You'll either have to remove the braces or upgrade your constant.

じ违心 2024-07-20 07:19:11

它在 perl5.8 上对我来说工作正常,但在 perl5.6 上不起作用。 错误信息是:

无法将“HASH(0x80c05d8)”定义为常量(名称包含无效字符或为空)

似乎是旧版本问题,可能是 Leon 描述

It works fine for me on perl5.8 but doesn't work on perl5.6 . The error message is:

Can't define "HASH(0x80c05d8)" as constant (name contains invalid characters or is empty)

Seems like an old-version issue, probably the issue that Leon described.

你怎么敢 2024-07-20 07:19:11

您的旧 Perl 可能不支持在一条语句中声明多个常量的语法。 尝试像这样单独定义每个常量......

use constant MAX_NET_DLR => 99.99;

Your old perl probably does not support the syntax where you declare multiple constants in one statement. Try defining each constant separately like this...

use constant MAX_NET_DLR => 99.99;
站稳脚跟 2024-07-20 07:19:11

我会使用 Readonly 模块。

常量的缺点是,它在调用者命名空间内创建一个返回常量值的函数。

Readonly 可用于将标量、数组和散列声明为只读(实常量)。

I would use the Readonly module.

The disadvantage of constant is, that it creates a function within the callers namespace which returns a constant value.

Readonly can be used to declare scalars, arrays and hashes readonly (real constants).

后eg是否自 2024-07-20 07:19:11

对我有用。 您确定该代码中的某处没有任何控制字符吗?

Works for me. Are you sure you don't have any control characters in that code somewhere?

注定孤独终老 2024-07-20 07:19:11

我很惊讶没有人建议这样做(这有点难看,实际上我用 Perl 5.18.2 测试了它):

use constant HASH => {
    a => 1,
    b => 2
};

BEGIN {
    while (my ($n, $v) = each %{(HASH)}) {
        eval "use constant $n => $v";
    }
}

对于长长的常量列表,这可能值得麻烦,但到今天为止,这都是历史性的。

I'm surprised that nobody suggested this (It's somewhat ugly, and I tested it with Perl 5.18.2 actually):

use constant HASH => {
    a => 1,
    b => 2
};

BEGIN {
    while (my ($n, $v) = each %{(HASH)}) {
        eval "use constant $n => $v";
    }
}

For long lists of constants this might be worth the trouble, but as of today that's all historic anyway.

影子的影子 2024-07-20 07:19:11

删除此行末尾的最后一个逗号:

PCT_THRES_CUST => 3,

这可能是问题所在。

Remove the last comma at the end of this line:

PCT_THRES_CUST => 3,

That could be the issue.

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