2

I'm using Strawberry Perl 5.14.2 on MS Windows. The following code runs fine on Linux Perl 5.10.1 but fails on MS Windows. I need to update it to run on both.

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);
}

It finds and parses $dictfile on MS Windows, but fails to accumulate the $freq{total}, which causes a divide-by-zero error elsewhere. $dictfile is a weighted dictionary with data that looks like this:

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

My troubleshooting between the two platforms shows it's failing either at split; or the following line, but I don't know enough Perl to fix it. Does the code need to be changed, or should I start Perl with a specific command-line option?

Thanks.

tahoar
  • 1,788
  • 3
  • 20
  • 36
  • 3
    Try to use an explicit `split` call, like `my @var = split;` and use `@var` instead of `@_` (maybe there is a hidden undocumented behaviour, but `split` in a void context doesn't use @_ to put there the splitted items) – ArtMat May 07 '12 at 09:49
  • Thanks ArtM. I turned on warnings and found that implicit splits are dedicated. Did just what you suggested and it works like a charm on both OS's and versions of Perl. – tahoar May 07 '12 at 10:01
  • 2
    [“Use of implicit split to @_ is deprecated”](http://stackoverflow.com/questions/2436160/why-does-perl-complain-use-of-implicit-split-to-is-deprecated) – daxim May 07 '12 at 11:40
  • `"$dictfile"` is excessive, just use `$dictfile`. – Brad Gilbert Oct 16 '12 at 16:13

1 Answers1

1

Per ArtM's suggestion, here's the working code.

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);
}
tahoar
  • 1,788
  • 3
  • 20
  • 36