Search code examples
perlperl-data-structures

Perl long data type


I am writing an app in Perl that requires long data type instead of integers. How can I achieve this. For example;

my $num = sprintf('%ld', 27823221234);

print $num;

The output is not a long, but an integer.


Solution

  • Here is some code that illustrates some of how Perl behaves - derived from your example:

    use strict;
    use warnings;
    
    my $num = sprintf("%ld", 27823221234);
    print "$num\n";
    
    my $val = 27823221234;
    my $str = sprintf("%ld", $val);
    printf "%d = %ld = %f = %s\n", $val, $val, $val, $val;
    printf "%d = %ld = %f = %s\n", $str, $str, $str, $str;
    

    With a 64-bit Perl, this yields:

    27823221234
    27823221234 = 27823221234 = 27823221234.000000 = 27823221234
    27823221234 = 27823221234 = 27823221234.000000 = 27823221234
    

    If you really need big number (hundreds of digits), then look into the modules that support them. For example: