perl中的标量变量是否有限制?

时间:2016-08-03 09:46:44

标签: perl mmap

现在我正在使用大文件,大小大于(1.5 GB)。所以我使用了File::Map。当输入大于2.1 GB脚本失败并显示错误Use of uninitialized value $count in print at file.pl line 16.时。

但脚本正在运行2.1GB and below 2.1GB

我的脚本如下

use warnings;
use strict;
use File::Map 'map_file';
my $filename ="/root/Desktop/test_sequence/human_genome";

map_file (my $map,$filename);

my $count;

$count++ while ($map=~/>/g);

print $count; #The file has only 14 `>` so result is 14.

同时我在没有模块的情况下尝试了它。它也以相同的错误终止。

use warnings;
use strict;
my $filename ="/root/Desktop/test_sequence/human_genome";

open my $fh,"<",$filename or die "$!";

my $m = do{local $/; <$fh>};

my $count;

$count++ while ($m=~/>/g);

print $count;

我不知道这里有什么问题?

2 个答案:

答案 0 :(得分:0)

问题确实是最大标量大小。从理论上讲,它可以在32位perl上达到4 GB,但由于你的地址空间有限,以适应操作系统之类的东西,2-3.5 GB通常是真正的限制。使用64位操作系统和perl它应该可以工作。

See this answer了解一些细节。

编辑:在这里运行正常,Fedora上的perl 5.22.2,x86_64-linux-thread-multi:

$ dd if=/dev/zero of=zero bs=1M count=5000
5000+0 records in
5000+0 records out
5242880000 bytes (5.2 GB) copied, 34.8694 s, 150 MB/s
$ perl -e 'sub wat{open my $fh, ">>zero" or die $!;
   seek($fh,shift,0); syswrite($fh, ">");}
   wat(1000);
   wat(100_000_000);
   wat(4_500_000_000);'
$ time perl map.pl 
3
real    0m5.638s
user    0m3.921s
sys     0m1.717s

答案 1 :(得分:-1)

您已在多个位置之一点击了有符号的32位整数限制

32位有符号值允许从0x1000_00000x7FFF_FFFF的值,即-2,147,483,648到2,147,483,647。因此你的2.1GB限制

我不知道这是否是您构建perl的限制,或者它是否属于File::Map

的一部分

任何大小的文件都可以通过一次读取一行来非常简单地处理,并且由于您的目的似乎只是计算Unicode GREATER-THAN SIGN字符的数量,您可以轻松地像这样做< / p>

use strict;
use warnings;

use constant HUMAN_GENOME => '/root/Desktop/test_sequence/human_genome';

my $count = do {

    open my $fh, '<', HUMAN_GENOME or die sprintf qq{Unable to open "%s": $!}, HUMAN_GENOME;

    my $n = 0;

    $n += tr/>// while <$fh>;

    $n;
};

print $count;