windows - 如何更新 Perl 脚本以在 MS Windows 上运行

标签 windows perl

<分区>

我在 MS Windows 上使用 Strawberry Perl 5.14.2。以下代码在 Linux Perl 5.10.1 上运行良好,但在 MS Windows 上运行失败。我需要更新它才能在两者上运行。

sub read_dict {
    open F, "<:utf8", "$dictfile" || die "Dictonary file $dictfile not found";
    while (<F>) {
        chomp;
        s/^ *//;
        split;
        $freq{$_[1]}  = $_[0];
        $header = substr($_[1],0,$wd);
        if ($freq{"m,$header"}) {
            if ($freq{"m,$header"} < length($_[1])) {
                $freq{"m,$header"} = length($_[1]);
            }
        } else {
            $freq{"m,$header"} = length($_[1]);
        }
        $freq{total} += $_[0];
    }
    close(F);
}

它在 MS Windows 上查找并解析 $dictfile,但未能累积 $freq{total},这会导致其他地方出现被零除错误。 $dictfile 是一个加权字典,其数据如下所示:

8 永垂不朽
8 震耳欲聋
85 罗马里奥
891 澳大利亚
9 埃芬贝格

我在两个平台之间进行的故障排除表明它在拆分时失败了;或以下行,但我对 Perl 的了解还不够,无法修复它。是否需要更改代码,或者我应该使用特定的命令行选项启动 Perl?

谢谢。

最佳答案

根据 ArtM 的建议,这是工作代码。

sub read_dict {
    open F, "<:utf8", "$dictfile" || die "Dictonary file $dictfile not found";
    while (<F>) {
        chomp;
        s/^ *//;
        my @entry = split(/ /, $_);
        $freq{$entry[1]}  = $entry[0];
        $header = substr($entry[1],0,$wd);
        if ($freq{"m,$header"}) {
            if ($freq{"m,$header"} < length($entry[1])) {
                $freq{"m,$header"} = length($entry[1]);
            }
        } else {
            $freq{"m,$header"} = length($entry[1]);
        }
        $freq{total} += $entry[0];
    }
    close(F);
}

关于windows - 如何更新 Perl 脚本以在 MS Windows 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479724/

相关文章:

java - 创建 Windows 10 持久通知

arrays - 如果满足条件,则向数组添加值

linux - 通过 sendmail 服务器发送邮件时遇到 sasl_decode 错误

Perl Map block 局部变量的使用

javascript - 如何修改 perl 变量并再次重新加载页面?

珀尔 : Dereference between @_ and $_

javascript - Node-Webkit 获取用户的桌面目录

c++ - _beginthread/ex C3861 - 找不到标识符

php - 在 XAMPP for Windows 中升级 PHP 5.4.0?

c - ucrtbased.dll 与 ucrtbase.dll