Data types#

Perl has three fundamental data types — scalars, arrays, and hashes — plus a small number of supporting kinds (references, typeglobs, code) that are technically scalars but behave differently enough to deserve their own treatment. Every value the language manipulates is one of these.

This reference is split into one page per topic. Topics that share context (numeric vs string conversion, references vs typeglobs, context vs interpolation) live in the same page; topics that don’t share context get their own page.

Choose a topic#

  • Scalars$x. Numbers, strings, references; undef; truth and definedness; the dual numeric/ string nature of scalars.

  • Arrays@arr. Element access, $#arr, negative indices, slices, list assignment, the @arr / $arr[i] / @arr[i,j] sigil rule.

  • Hashes%h. Key-value pairs, the four flavours of subscript, keys/values/each, key ordering, hash slices and key/value slices.

  • Context — list, scalar, void, boolean. How each operator and assignment imposes context, what wantarray reports, the scalar() operator, and the recurring traps with , versus list construction.

  • Numbers and strings — automatic conversion both ways, integer vs float, Inf and NaN, numeric literal forms, «0 but true», dualvar, locale and precision pitfalls.

  • References\X, the seven things you can reference, the four ways to deref, autovivification, weak references, circular structures, ref and Scalar::Util.

  • Typeglobs*name. The symbol-table entry, aliasing, *foo{THING}, when modern Perl still needs them and when references are the right answer instead.

  • Interpolation — what double-quoted strings, here-docs, and regex patterns interpolate; the ${name} braces; the @{[ expr ]} and ${\ expr } baroque constructs; ambiguities with [ and { after a sigil.

Cross-cutting concepts#

A handful of ideas appear on every page below. Read these once and the rest will feel familiar:

  • Sigils mark the result, not the container. @arr[1, 3] is not «two arrays»; the @ says «the result is a list». The variable named arr is the same in @arr, $arr[0], @arr[0..3], and %arr[0..3].

  • Context coerces. A scalar evaluated in list context becomes a one-element list; a list evaluated in scalar context yields its last element (comma operator), but an array in scalar context yields its length. The two cases are not the same; see context.

  • References are scalars. A reference to anything fits in a scalar variable, which is what makes nested data structures possible. The deref syntax (@$ref, $ref->[0], ${$ref}{key}) is the inverse of \.

  • undef is a value, not the absence of one. A scalar can hold the value undef; testing defined $x is how you ask. An array can have undef elements without being empty.

  • Strings and numbers convert silently. "42" + 1 is 43, 42 . "x" is "42x". The conversion is always defined; see numbers and strings for what counts as «looks like a number» and where the corners are.

See also#

  • perlop — the operators that read and produce these values (arithmetic, comparison, subscript, range, arrow, assignment).

  • perlvar — the special variables Perl populates with state from the last operation, including $_, @_, the match variables, and the I/O-shaping $/, $\, $,, $".

  • perlfunc — the built-in functions that operate on these types: ref, scalar, wantarray, defined, keys, values, each, exists, delete, bless.

  • References tutorial — step-by-step introduction to the reference forms covered tersely in references.