In Perl, a scalar variable is a pointer to a C structure called SV . This includes various fields for metadata, such as a reference counter, a bit field that determines the exact type, and a pointer to additional (meta data).
If you use a scalar as an integer, it is called IV and contains an integer. The size of this integer is fixed when compiling perl . You can look at the output of perl -V to see the size of various data types. I have ivsize=8 . The values presented are the same as for integer C of this size.
If you use a scalar as a decimal, it is called NV (numeric value) and usually contains double. Again, the exact size is determined at compile time.
If you use a scalar as a string, it is called PV and contains a pointer to the string C, as well as some additional metadata, such as length. Line C is redistributed if it grows.
If you use a scalar as a string and as a number, it is PVIV or PVNV respectively. and includes data of both types.
Additional types exist, such as links ( RV ) or unsigned integers ( UV ).
For IV and NV Perl does not automatically increment numbers to bignums when they become large enough.
Then there are HV hashes and AV arrays. They use the SV header for things like reference counting, but point to more complex data structures.
Arrays contain an array of C pointers to SV s. If the array grows, it is redistributed.
Hashes are much more complicated. In principle, they are also an array, but contain hash entries instead of SV s. Elements of this hash are called buckets. If the ratio of "inputs" to buckets is too large, the array is redistributed (usually in double size) and items recently distributed over these buckets. This is not strictly necessary, but if this is not done, then search for O(n) instead of O(1) (i.e., Slow).
Variable-sized data structures such as strings, arrays, hashes are conservatively allocated initially. If more space is required, then most of the memory is allocated and the data is copied.
Scalars have a constant size header. Additional memory for additional metadata is allocated when the type is changed (for example, via a structure).
For more information and confusing pointer diagrams, read Illustrated Perl Guts .
amon
source share