Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Thanks to everyone for their interesting and instructive responses.

For completeness, some candidates tried using symrefs, via something like this:

use strict; use warnings; sub Invoker::first_callback { my $z = shift; print "in first_callback, z=$z\n"; return 1; } sub Invoker::last_callback { my $z = shift; print "in last_callback, z=$z\n"; return 2; } sub invoker { my ($name, $z) = @_; my $handler = 'Invoker::' . $name . "_callback"; no strict 'refs'; my $rc = eval { &$handler($z) }; return defined($rc) ? $rc : -1; } # Main program for testing --------------------------------- for my $name ( "first", "last", "fred" ) { my $rc = invoker( $name, $name . '-arg' ); print "$name: rc=$rc\n"; }
or this:
use strict; use warnings; package Invoker; sub first_callback { my $z = shift; print "in first_callback, z=$z\n"; return 1; } sub last_callback { my $z = shift; print "in last_callback, z=$z\n"; return 2; } sub invoker { my ($name, $z) = @_; my $handler = $name . '_callback'; no strict 'refs'; exists(${__PACKAGE__.'::'}{$handler}) or return -1; &$handler($z); } package main; # Main program for testing --------------------------------- for my $name ( "first", "last", "fred" ) { my $rc = Invoker::invoker( $name, $name . '-arg' ); print "$name: rc=$rc\n"; }

For cheap thrills, just now I tried implementing via Class::MOP. I'm a Class::MOP ignoramus, so please feel free to suggest better ways to do it.

use strict; use warnings; use Class::MOP; # Callback functions --------------------------------------- sub first_callback { my $z = $_[1]; print "in first_callback, z=$z\n"; return 1; } sub last_callback { my $z = $_[1]; print "in last_callback, z=$z\n"; return 2; } # Implementation of dispatch table ------------------------- my $invoker_pkg = Class::MOP::Package->create('Invoker'); $invoker_pkg->add_package_symbol('&first', \&first_callback); $invoker_pkg->add_package_symbol('&last', \&last_callback); # use Data::Dumper; # my $r = $invoker_pkg->namespace(); warn Dumper($r); sub invoker { my $name = shift; $invoker_pkg->has_package_symbol('&' . $name) or return -1; Invoker->$name(@_); } # Main program for testing --------------------------------- for my $name ( "first", "last", "fred" ) { my $rc = invoker( $name, $name . '-arg' ); print "$name: rc=$rc\n"; }

In reply to Re: Rosetta Dispatch Table by eyepopslikeamosquito
in thread Rosetta Dispatch Table 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (3)
    As of 2020-07-09 20:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?