Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Rosetta Dispatch Table

by tybalt89 (Monsignor)
on Nov 21, 2017 at 23:07 UTC ( [id://1203966]=note: print w/replies, xml ) Need Help??


in reply to Rosetta Dispatch Table (Interview Question)

Why create your own dispatch table when perl has one built in?

Or - violating specifications for fun? and profit?

#!/usr/bin/perl # http://perlmonks.org/?node_id=1203952 use strict; use warnings; # Callback functions --------------------------------------- sub first::callback { my $z = pop; print "in first_callback, z=$z\n"; return 1; } sub last::callback { my $z = pop; print "in last_callback, z=$z\n"; return 2; } sub UNIVERSAL::callback { -1 } # Implementation of dispatch table ------------------------- # (You need to write this code) #my %op_table = ( first => \&first_callback, # last => \&last_callback, # ); sub invoker { my ($name, $z) = @_; $name->callback($z); } # Main program for testing --------------------------------- for my $name ( "first", "last", "fred" ) { my $rc = invoker( $name, $name . '-arg' ); print "$name: rc=$rc\n"; }

Have I once again managed to fail an interview?

Replies are listed 'Best First'.
Re^2: Rosetta Dispatch Table
by eyepopslikeamosquito (Archbishop) on Nov 21, 2017 at 23:29 UTC

    That's the sort of ingenuity and inventiveness I was hoping to provoke. Thanks.

    Have I once again managed to fail an interview?
    I think we both know I would be delighted to offer you a job without requiring an interview ... though I understand you are no longer looking. :)

Re^2: Rosetta Dispatch Table
by LanX (Saint) on Nov 22, 2017 at 16:53 UTC
    some annotations:
    • I would be cautious about name spaces like first:: and last:: , if you really need extra packages I'd use an extra parent one like callback::first etc
    • I'd use ->can beforehand to check if the callback is really available, to avoid an unexpected die without good notice
    • the STASH - your built in dispatch table - is a hash , just global (which leads to the first remark :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

      Is there a case that ->can is needed that is not caught by UNIVERSAL::callback ?

        Is there a case that ->can is needed that is not caught by UNIVERSAL::callback ?

        No, afaik; it rather catches too much. If some package ought to have a callback method which it hasn't due to some fubar, UNIVERSAL::callback kicks in - long faces, head-scratching. But! if someone writes a method/function for package UNIVERSAL, they should know what they are doing, and my bets are that you do.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1203966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found