mkHun mkHun - 3 months ago 8
Perl Question

Is there any limit for scalar variable in perl?

Now I'm working with large file, which size is greater than (1.5 GB). So I used

File::Map
. When the input is greater than
2.1 GB
script failed with showing error
Use of uninitialized value $count in print at file.pl line 16.
.

But script is running
2.1GB and below 2.1GB


My script is as below

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.


At the same time I tried it without the module. It is also terminate with same error.

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;


I don't know what is the problem here?

Answer

You have hit a signed 32-bit integer limit in one of several places

A 32-bit signed value allows for values from 0x1000_0000 to 0x7FFF_FFFF, which is -2,147,483,648 to 2,147,483,647. Hence your 2.1GB limit

I don't know whether that is a limit of your build of perl, or if it is part of File::Map

Files of any size can be handled very simply by reading one line at a time, and since your purpose here seems to be just to count the number of Unicode GREATER-THAN SIGN characters, you could do it easily like this

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;
Comments