Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 43 ยท References and Data

Nested Data Structures

Build and walk nested Perl data: arrays of hashes, hashes of arrays, deep copies with Storable, debugging with Data::Dumper and JSON::PP encode and decode.

Real data is rarely flat. A list of people each has a name and several skills. A configuration file has sections, and each section has settings. In Perl you model this by putting references inside arrays and hashes, which gives you arrays of hashes, hashes of arrays and anything deeper. If references are still new to you, read References first.

This lesson shows the common shapes, how to loop over them, how to copy them safely, and how to print or save them with the core modules Data::Dumper, JSON::PP and Storable.

Perl is not installed on this site, so the Perl code is read-only and its output was captured from a real run. The interactive examples are Python stand-ins (lists and dicts behave like Perl's nested arrays and hashes).

The four everyday shapes

Pick the shape that fits

Array of hashes A list of records, like rows.
Hash of arrays Each key owns a list, like groups.
Hash of hashes Lookup by key, then by field.
Array of arrays A grid or table of cells.

All four are just references inside containers.

1 / 2

Array of hashes

This is the shape of a database result: one hash per record, kept in order.

PERL
use v5.36;

my @people = (
    { name => 'Ana', age => 31, langs => ['perl', 'c'] },
    { name => 'Ben', age => 27, langs => ['go'] },
);

for my $person (@people) {
    say "$person->{name} knows ", join(', ', $person->{langs}->@*);
}
say scalar(@people), " people, last index ", $#people;
TEXT
Ana knows perl, c
Ben knows go
2 people, last index 1

Notice $person->{langs}->@*: the hash holds an array reference, and ->@* turns it back into a list. You can reach one value directly with $people[0]{langs}[1], which is c. The arrow is optional between subscripts.

Sorting records uses a comparison on a field:

PERL
my @by_age = sort { $a->{age} <=> $b->{age} } @people;
say $by_age[0]{name};    # Ben

Hash of arrays

When you group items by a key, each key owns a list. Autovivification does the setup, so push just works.

PERL
my %by_lang;
for my $person (@people) {
    push $by_lang{$_}->@*, $person->{name} for $person->{langs}->@*;
}

for my $lang (sort keys %by_lang) {
    say "$lang: @{ $by_lang{$lang} }";
}
TEXT
c: Ana
go: Ben
perl: Ana

The first time a language appears, $by_lang{$lang} is undefined, and push creates an empty array there. There is no need for an if (!exists ...) check.

Hash of hashes and array of arrays

A hash of hashes gives two-step lookups: $stock{apple}{count}. An array of arrays models a grid: $grid[1][2] is row 1, column 2. Both are built the same way, with anonymous { } and [ ].

PERL
my %stock = (
    apple => { count => 12, price => 0.5 },
    pear  => { count => 4,  price => 0.75 },
);
my @grid = ([1, 2, 3], [4, 5, 6]);

say $stock{pear}{price};   # 0.75
say $grid[1][2];           # 6
say scalar @{ $grid[0] };  # 3

Here is the same idea in a runnable stand-in. Python lists and dicts nest the same way.

Python Stand-in: grouping records by key

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

setdefault plays the role of Perl's autovivification here. Python does not create missing keys on its own, so you ask for it explicitly.

Walking a structure of unknown shape

When you do not know how deep the data goes, write a recursive subroutine and use ref to decide what to do at each level.

PERL
use v5.36;

sub walk ($data, $indent = 0) {
    my $pad = '  ' x $indent;
    if (ref $data eq 'HASH') {
        for my $key (sort keys %$data) {
            say "$pad$key:";
            walk($data->{$key}, $indent + 1);
        }
    }
    elsif (ref $data eq 'ARRAY') {
        walk($_, $indent) for @$data;
    }
    else {
        say "$pad- $data";
    }
}

walk({ fruit => ['apple', 'pear'], veg => { root => 'carrot' } });
TEXT
fruit:
  - apple
  - pear
veg:
  root:
    - carrot

The same pattern, recursion plus a type check, powers deep comparison, pretty printers and search tools. Recursion itself is covered in Scope, Closures and Recursion.

Python Stand-in: a recursive walk

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Copying: shallow versus deep

Copying a nested structure is a classic trap. A plain array copy duplicates only the top level, so the inner hashes are still shared.

PERL
my @copy = @people;          # shallow copy
$copy[0]{name} = 'Zed';
say $people[0]{name};        # Zed  (the original changed too!)

For an independent copy use dclone from the core module Storable, which clones the whole structure, however deep.

PERL
use Storable qw(dclone);

my $deep = dclone(\@people);
$deep->[1]{name} = 'Yan';
say $people[1]{name};        # Ben  (the original is safe)

dclone works on plain data and objects, but not on code references. For simple cases you can also copy by hand level by level: my %row_copy = %$row;.

Python Stand-in: shallow vs deep copy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Printing structures with Data::Dumper

print @people shows useless addresses like HASH(0x...). Data::Dumper is the standard debugging aid: it turns any structure into readable Perl source text.

PERL
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;   # stable key order
$Data::Dumper::Indent   = 1;   # compact indentation

print Dumper($people[1]);
TEXT
$VAR1 = {
  'age' => 27,
  'langs' => [
    'go'
  ],
  'name' => 'Ben'
};

Setting $Data::Dumper::Terse = 1 drops the $VAR1 = prefix. Always turn on Sortkeys, because hash order is not stable between runs and your output would otherwise jump around.

JSON with JSON::PP

JSON is the format almost every web service speaks. JSON::PP ships with Perl, and it maps JSON objects to hashes, arrays to arrays, strings and numbers to scalars, and null to undef.

PERL
use JSON::PP;

my $json = JSON::PP->new->canonical->pretty;
print $json->encode({
    name   => 'Ana',
    langs  => ['perl', 'c'],
    active => JSON::PP::true,
    score  => 9.5,
});
TEXT
{
   "active" : true,
   "langs" : [
      "perl",
      "c"
   ],
   "name" : "Ana",
   "score" : 9.5
}

The canonical option sorts keys so the output is repeatable, and pretty adds indentation. Decoding goes the other way:

PERL
my $data = decode_json('{"id":7,"tags":["a","b"],"ok":true,"none":null}');

say $data->{id};            # 7
say $data->{tags}[1];       # b
say $data->{ok} ? 'yes' : 'no';   # yes
say defined $data->{none} ? 'set' : 'undef';   # undef

Two details to remember. First, the functions encode_json and decode_json work with UTF-8 bytes; the object interface (JSON::PP->new) lets you choose with or without ->utf8. Second, whether 3.5 comes out as a number or "3.5" as a string depends on how the scalar was last used in Perl, so use 0 + $value to force a number. Booleans decode to special JSON::PP::Boolean objects that behave as true and false.

Python Stand-in: encode and decode JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Saving structures with Storable

When the data only needs to be read back by another Perl program, Storable saves a structure to a file in a fast binary format.

PERL
use Storable qw(store retrieve);

store(\@people, 'people.sto');
my $back = retrieve('people.sto');
say $back->[0]{name};

Use JSON instead when other languages or humans need to read the file. Never retrieve files from untrusted sources: the format can rebuild objects, which is a security risk (see Security and Safe Perl).

Common mistakes

  • Using @$x on an undefined value under strict. Reading @{ $h{list} } when the key is missing dies with "Can't use an undefined value as an ARRAY reference". Use @{ $h{list} // [] } for a safe default.
  • Assuming hash order. Sort keys when order matters.
  • Shallow-copying nested data. Reach for dclone.
  • Checking exists only on the last key. exists $h{a}{b} creates $h{a}. Check each level if that matters.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you read the second language of the first person in @people, where each person has a langs array reference?

    @people is an array, so the first element is $people[0]. It is a hash, so {langs} follows, and the langs value is an array reference indexed with [1].
  2. 2. Why does my @copy = @people; $copy[0]{name} = 'Zed'; also change $people[0]{name}?

    Array assignment copies the elements, and each element is a reference. Both arrays therefore point at the same inner hashes.
  3. 3. Which core function makes a fully independent copy of a nested structure?

    dclone from Storable recursively clones the whole structure. Dumper and encode_json only produce text, and weaken manages reference counts.
  4. 4. What does Data::Dumper do, and which setting makes its output order stable?

    Dumper shows data structures as readable text. Hash key order varies, so Sortkeys = 1 gives the same output every run.
  5. 5. In JSON::PP, what does a JSON null decode to?

    JSON null maps to Perl's undef, which is why you check with defined.

Summary

  • Nested data is built from references: arrays of hashes, hashes of arrays, hashes of hashes and arrays of arrays.
  • Chain subscripts like $people[0]{langs}[1]; use ->@* and ->%* to get whole containers.
  • Autovivification lets push $h{key}->@*, $x build groups without setup code.
  • Write a recursive walker with ref for data of unknown depth.
  • dclone gives a deep copy; plain assignment copies only the top level.
  • Data::Dumper (set Sortkeys) for debugging, JSON::PP for exchange, Storable for Perl-only storage.

Next, organize your code into reusable files in Packages and Modules.