References and Nested Data
Hash References
A hash reference points to a hash, and ->{key} reads one value from it.
hash reference
A hash reference is a scalar value that points to a hash.
Hash References
hash_refs.pl
Replay: real traced execution (multi-file project)
use strict;
use warnings;
my $key = "perl";
my $scores_ref = { perl => 92, go => 88 };
my $exists = exists $scores_ref->{$key};
my $value = $exists ? $scores_ref->{$key} : 0;
my $status = $exists ? "found" : "missing";
print "key=$key\n";
print "status=$status\n";
print "value=$value\n";
use strict;
use warnings;
my $key = "go";
my $scores_ref = { perl => 92, go => 88 };
my $exists = exists $scores_ref->{$key};
my $value = $exists ? $scores_ref->{$key} : 0;
my $status = $exists ? "found" : "missing";
print "key=$key\n";
print "status=$status\n";
print "value=$value\n";
use strict;
use warnings;
my $key = "ruby";
my $scores_ref = { perl => 92, go => 88 };
my $exists = exists $scores_ref->{$key};
my $value = $exists ? $scores_ref->{$key} : 0;
my $status = $exists ? "found" : "missing";
print "key=$key\n";
print "status=$status\n";
print "value=$value\n";
$key ← perl, $exists ← 1, $value ← 92, $status ← found
4my $key→ perl = "perl"; #@key="go", "ruby"5my $scores_ref = { perl => 92, go => 88 };6my $exists→ 1 = exists $scores_ref->{$key};7my $value→ 92 = $exists1 ? $scores_ref->{$key} : 0;8my $status→ found = $exists1 ? "found" : "missing";910print "key=$keyperl\n";11print "status=$statusfound\n";12print "value=$value92\n";outputkey=perl status=found value=92
$key ← go, $exists ← 1, $value ← 88, $status ← found
4my $key→ go = "go";5my $scores_ref = { perl => 92, go => 88 };6my $exists→ 1 = exists $scores_ref->{$key};7my $value→ 88 = $exists1 ? $scores_ref->{$key} : 0;8my $status→ found = $exists1 ? "found" : "missing";910print "key=$keygo\n";11print "status=$statusfound\n";12print "value=$value88\n";outputkey=go status=found value=88
$key ← ruby, $exists ← (empty), $value ← 0, $status ← missing
4my $key→ ruby = "ruby";5my $scores_ref = { perl => 92, go => 88 };6my $exists→ (empty) = exists $scores_ref->{$key};7my $value→ 0 = $exists(empty) ? $scores_ref->{$key} : 0;8my $status→ missing = $exists(empty) ? "found" : "missing";910print "key=$keyruby\n";11print "status=$statusmissing\n";12print "value=$value0\n";outputkey=ruby status=missing value=0