Bonus (got the idea by reading tybalt89's version)

sub Invoker::first { shift; &first_callback } sub Invoker::last { shift; &last_callback } sub Invoker::AUTOLOAD { -1 } sub invoker { my $name = shift; Invoker->$name(@_); }
It looks and kind of quacks like a symbolic ref on the method name, except it works under strict and warnings.

Edit: Actually if you ignore the first parameter of the callbacks, you can just use the symbols table as the containing hash and use the method above :)

use strict; use warnings; # Callback functions --------------------------------------- sub first_callback { my (undef, $z) = @_; print "in first_callback, z=$z\n"; return 1; } sub last_callback { my (undef, $z) = @_; print "in last_callback, z=$z\n"; return 2; } sub default_callback { -1 } # Dispatch ------------------------------------------------- %Dispatch:: = ( first => *first_callback, start => *first_callback, last => *last_callback, AUTOLOAD => *default_callback); sub invoker { my $name = shift; Dispatch->$name(@_); } # Main program for testing --------------------------------- for my $name ( qw< first start last fred > ) { my $rc = invoker( $name, $name . '-arg' ); print "$name: rc=$rc\n"; }


In reply to Re^2: Rosetta Dispatch Table by Eily
in thread Rosetta Dispatch Table (Interview Question) by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.