Nested references let a program group related values inside larger data structures.

nested data Nested data stores one structure inside another structure.

Nested Lookup

user
nested_lookup.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;

my $user = "ana";
my $profiles = {
    ana => { city => "Oslo", points => 4 },
    ben => { city => "Lima", points => 7 },
};
my $profile = $profiles->{$user};
my $city = $profile ? $profile->{city} : "none";
my $points = $profile ? $profile->{points} : 0;

print "user=$user\n";
print "city=$city\n";
print "points=$points\n";
use strict;
use warnings;

my $user = "ben";
my $profiles = {
    ana => { city => "Oslo", points => 4 },
    ben => { city => "Lima", points => 7 },
};
my $profile = $profiles->{$user};
my $city = $profile ? $profile->{city} : "none";
my $points = $profile ? $profile->{points} : 0;

print "user=$user\n";
print "city=$city\n";
print "points=$points\n";
use strict;
use warnings;

my $user = "cy";
my $profiles = {
    ana => { city => "Oslo", points => 4 },
    ben => { city => "Lima", points => 7 },
};
my $profile = $profiles->{$user};
my $city = $profile ? $profile->{city} : "none";
my $points = $profile ? $profile->{points} : 0;

print "user=$user\n";
print "city=$city\n";
print "points=$points\n";
  1. $user ← ana, $city ← Oslo, $points ← 4

    4my $user→ ana = "ana"; #@user="ben", "cy"5my $profiles = {6    ana => { city => "Oslo", points => 4 },7    ben => { city => "Lima", points => 7 },8};9my $profile = $profiles->{$user};10my $city→ Oslo = $profile ? $profile->{city} : "none";11my $points→ 4 = $profile ? $profile->{points} : 0;1213print "user=$userana\n";14print "city=$cityOslo\n";15print "points=$points4\n";
    outputuser=ana
    city=Oslo
    points=4
  1. $user ← ben, $city ← Lima, $points ← 7

    4my $user→ ben = "ben";5my $profiles = {6    ana => { city => "Oslo", points => 4 },7    ben => { city => "Lima", points => 7 },8};9my $profile = $profiles->{$user};10my $city→ Lima = $profile ? $profile->{city} : "none";11my $points→ 7 = $profile ? $profile->{points} : 0;1213print "user=$userben\n";14print "city=$cityLima\n";15print "points=$points7\n";
    outputuser=ben
    city=Lima
    points=7
  1. $user ← cy, $profile ← (empty), $city ← none, $points ← 0

    4my $user→ cy = "cy";5my $profiles = {6    ana => { city => "Oslo", points => 4 },7    ben => { city => "Lima", points => 7 },8};9my $profile→ (empty) = $profiles->{$user};10my $city→ none = $profile(empty) ? $profile->{city} : "none";11my $points→ 0 = $profile(empty) ? $profile->{points} : 0;1213print "user=$usercy\n";14print "city=$citynone\n";15print "points=$points0\n";
    outputuser=cy
    city=none
    points=0