In other languages besides Perl when you declare an integer it has minimum and maximum values based on the amount of space in memory the variable is taking up. When you declare a scalar variable in Perl, whether it be a number or string, does the language only allocate enough for the variable value and then increase the space if necessary later or does Perl allocate a large amount of memory initially?
Scalar variables are used to represent individual fixed-size data objects, such as integers and pointers. Scalar variables can also be used for fixed-size objects that are composed of one or more primitive or composite types. D provides the ability to create both arrays of objects as well as composite structures.
A variable containing a single value is a scalar variable. Other times, it is convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an list variable.
A scalar variable stores a value with no internal components. The value can change. A scalar variable declaration specifies the name and data type of the variable and allocates storage for it. The declaration can also assign an initial value and impose the NOT NULL constraint.
In Perl, a scalar variable is a pointer to a C struct called an SV
. This includes various fields for metadata like the reference count, a bitfield that determines the exact type, and a pointer to additional (meta-)data.
If you use a scalar as an integer, it is called an IV
and contains an integer. The size of this integer is fixed at compilation of perl
. You can look at the perl -V
output to view the size of various data types. I have ivsize=8
. The representable values are the same as for the C integer of that size.
If you use a scalar as a decimal, it is called an NV
(numerical value) and contains a double, usually. Again, the exact size is determined at compile time.
If you use a scalar as a string, it is called a PV
and contains a pointer to a C string, plus some additional metadata like length. The C string is reallocated if it grows.
If you use a scalar as a string and as a number, it is a PVIV
or PVNV
resp. and includes the data of both types.
There are additional types like references (RV
) or unsigned integers (UV
).
For the IV
and NV
, Perl does not automatically promote the numbers to bignums when they grow large enough.
Then there are hashes HV
and arrays AV
. These use the SV
header for things like reference counting but point to more complicated data structures.
Arrays contain a C array of pointers to SV
s. If the array grows, it is reallocated.
Hashes are far more complex. Basically, they are an array as well, but contain hash entries instead of SV
s. The elements in this hash are called buckets. If the entries-to-buckets ratio is too high, the array is reallocated (usually to double size) and the entries newly distributed across these buckets. This isn't strictly neccessary, but if this isn't done then lookup is O(n)
instead of O(1)
(i.e. slow).
Variable sized data structures like strings, arrays, hashes are initially allocated conservatively. If more space is required, then a larger piece of memory is allocated, and the data copied over.
Scalars have a constant-sized header. Additional memory for additional metadata is allocated when the type changes (e.g. through stringification).
For more information and confusing pointer diagrams read the Illustrated Perl Guts.
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