Resolve a media type
Before you start this advanced track
Arrays, hashes, and regular expressions. Expect sigils, capture groups, and scalar/list context.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
A Perl hash stores values under named keys. The whole hash uses a percent sign, while one scalar value uses a dollar sign and braces around the key. An extension variable can supply that key dynamically. This keeps the same lookup expression useful for many different document extensions.
use strict;use warnings;my %types = (svg => "image/svg+xml", txt => "text/plain");my $extension = "svg";my $media_type = $types{$extension};print "$media_type\n";Each mission is a complete Perl 5 script. Run one with perl. The scripts enable strict and warnings and use only built-in features; no modules, files, or external commands are needed.
Track the values line by line. Predict the result before running this example in your own editor.