#!/usr/bin/perl -s use strict; use warnings; use 5.014; our $h; sub callback; our %CALLBACKS; # Callback functions --------------------------------------- callback first => sub { my $z = shift; print "in first_callback, z=$z\n"; return 1; }; callback last => sub { my $z = shift; print "in last_callback, z=$z\n"; return 2; }; # Implementation of dispatch table ------------------------- # (You need to write this code) sub callback { my ($name, $cb) = @_; $CALLBACKS{$name} = $cb; } sub invoker { my ($name, $z) = @_; return -1 unless exists($CALLBACKS{$name}); $CALLBACKS{$name}->($z); } sub help { say "Available callbacks: @{[ sort keys %CALLBACKS ]}"; } # Main program for testing --------------------------------- exit help() if $h; for my $name ( "first", "last", "fred" ) { my $rc = invoker( $name, $name . '-arg' ); print "$name: rc=$rc\n"; }