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
$configis a hashref holding a string, an arrayref, and another hashref.- Chained subscripts drill in:
$config->{ports}[0],$config->{db}{host}. - Arrows between subscripts are optional; only the first one is required.
- Nested values are lvalues too:
+=updatespoolright where it lives.
Keywords and builtins used here
myprintuse
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.
Step 3 of 3 in References & structures, step 21 of 27 in Language basics.