4

The issue I have can be found by running the following code in Strawberry perl 5.12.3.0 on Windows XP.

    #!/usr/bin/perl -w

    use strict;
    use warnings;
    use Win32::Unicode::File;
    use Encode;

    my $fname = shift @ARGV;

    my $fh = Win32::Unicode::File->new;
    if ($fh->open('<', $fname)){
      while (my $line = $fh->readline()){}
      close $fh;
    }else{
      print "Couldn't open file: $!\n";
    }

The only thing that is happening here is that I perform a readline and this keeps eating memory until I get an Out of memory error from Strawberry perl. I am using a really big file but since this code is stream based it shouldn't matter. Am I missing something here or is there a leak somewhere in Strawberry perl? I tested the exactly same code in ActivePerl and there it works fine, i.e., it doesn't eat memory.

Update: Replacing Win32::Unicode::File with the normal diamond operator seems to work on my distribution at least. See the following code.

    use strict;
    use warnings;

    my $fname = shift @ARGV;

    if (open(my $fh, '<', $fname)){
      while (my $line = <$fh>){}
      close $fh;
    }else{ print "Couldn't open file: $!\n";}

So that would suggest the problem lies with Win32::Unicode module right?

4

2 回答 2

1

也许 $/(或 $INPUT_RECORD_SEPARATOR)不是新行?或 $[(第一个数组元素的索引和(子)字符串中的第一个字符)不为 0。

模块在读取或读取行期间使用这两个变量。

顺便说一句:这太慢了,因为它使用 3 个函数调用来一次读取每一行一个字符,然后为每个读取的字符调用 Encode::decode ,然后将其添加到 readline 返回到您的代码的行缓冲区中。呸!

于 2012-01-27T04:31:16.633 回答
1

我想有点不正统,但我会回答我自己的问题。我已经用 Path::Class::Unicode 包替换了 Win32::Unicode::File 包,而不是读取 unicode 文件。这工作正常(即没有内存消耗),所以看起来问题出在 Win32::Unicode::File 包中,很可能是一个错误。我已经联系了包裹的作者,他正在调查它。如果您希望我提供代码,请告诉我。这很简单。

于 2012-01-30T21:18:33.063 回答