While, for example, perldata documents that scalar strings in Perl are limited only by available memory, I'm strongly suspecting in real life there would be some other limits.
I'm considering the following ideas:
2 ** 31
, 2 ** 32
, 2 ** 63
or 2 ** 64
bytes.So, what would be the other factors that limit Perl string length in real life? What should be considered an okay string length for practical purposes?
So, we can have a String with the length of 2,147,483,647 characters, theoretically. Let's find the maximum length of the string through a Java program.
length() function in Perl finds length (number of characters) of a given string, or $_ if not specified. Return: Returns the size of the string.
The language specification requires strings to have a maximum length of 253 - 1 elements, which is the upper limit for precise integers.
It keep track of the size of the buffer and the number of bytes therein.
$ perl -MDevel::Peek -e'$x="abcdefghij"; Dump($x);'
SV = PV(0x9222b00) at 0x9222678
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x9238220 "abcdefghij"\0
CUR = 10 <-- 10 bytes used
LEN = 12 <-- 12 bytes allocated
On a 32-bit build of Perl, it uses 32-bit unsigned integer for these values. This is (exactly) large enough to create a string that uses up your process's entire 4 GiB address space.
On a 64-bit build of Perl, it uses 64-bit unsigned integer for those values. This is (exactly) large enough to create a string that uses up your process's entire 16 EiB address space.
The docs are correct. The size of the string is limited only by available memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With