AnyCompiler

Closing Thoughts

This course covered a language that genuinely does things differently from everything else on this site: sigils that encode data type directly into a variable's name ($scalar, @array, %hash); context, where the very same expression means something different depending on whether Perl wants one value or a list of them; regular expressions built into the language's own grammar rather than bolted on as a library; and references, which let Perl's three flat core types combine into arbitrarily deep nested structures.

That last idea — references stitching flat types into something richer — is worth seeing combined one more time: an array of hash references, iterated with foreach, reaching into each one with the -> arrow, and reduced with map into a plain list.

use strict;
use warnings;

my @inventory = (
    { name => "Widget", qty => 4 },
    { name => "Gadget", qty => 7 },
);

my $total = 0;
$total += $_->{qty} for @inventory;

my @names = map { $_->{name} } @inventory;

print "Items: @names\n";
print "Total quantity: $total\n";
Output
Items: Widget Gadget
Total quantity: 11

That's the core of idiomatic Perl: sigils and context doing a lot of quiet, implicit work, regex and list-processing functions doing the heavy lifting a loop would otherwise need, and references holding the whole structure together underneath. More lessons are added regularly — thanks for working through this one.

Practice exercise

Given my %scores = (Alice => 90, Bob => 75, Carol => 88), print each name and score, highest score first, one per line as "Name: Score".

← Error HandlingMore lessons coming soon