typestar

Nested data structures in Perl

Anonymous [] and {} nest to any depth, like JSON before JSON.

use strict;
use warnings;

# anonymous [] and {} nest to any depth
my $config = {
    name  => "typestar",
    ports => [8080, 8443],
    db    => { host => "localhost", pool => 5 },
};

print "name: $config->{name}\n";
print "first port: $config->{ports}[0]\n";
print "db host: $config->{db}{host}\n";

$config->{db}{pool} += 5;
print "pool now: $config->{db}{pool}\n";

How it works

  1. $config is a hashref holding a string, an arrayref, and another hashref.
  2. Chained subscripts drill in: $config->{ports}[0], $config->{db}{host}.
  3. Arrows between subscripts are optional; only the first one is required.
  4. Nested values are lvalues too: += updates pool right where it lives.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
358
Tokens
68
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 63 seconds.

Type this snippet

Step 3 of 3 in References & structures, step 21 of 27 in Language basics.

← Previous Next →