Best viewed with a modern CSS2-compliant browser, such as Mozilla Firefox.

Perl 5 Semantics

Arrays and hashes

The next two types of containers are the aggregates: arrays and hashes. An aggregate is a container whose purpose is to aggregate other containers, hence the name.

An array contains zero or more elements, each referring to a scalar container (except for gaps that may occur due to element deletion). Note that the element-containers exist separately from the aggregate.

When accessing a non-existent element, you get undef if you just read it, but the element is created with a new scalar container if you try to modify it or take a reference to it. Using some magic, this decision may be deferred until some time after you obtain the element.

Accessing a non-existent element by any way other than direct indexing or slicing ($y[2], @y[1,2]) — such as looping over an array with gaps, or using shift — will never auto-create but will yield the unique container returned by undef.

Normally array elements will always be freshly created containers, but it is possible to get existing containers inside an array: when you call a sub, the argument array is populated with the containers that were passed as arguments. For example, when you call foo($x, 42) then $_[0] inside foo() and $x will be aliases.

Hashes work almost the same as arrays. The main differences are that the elements are accessed by arbitrary key-strings rather than small integer indices, the order of elements is undefined, and gaps do not exist. Also, I'm not aware of any way to place existing containers inside a hash, other than via modules such as Data::Alias.