in reply to Subroutines in Dispatch Tables

Anonymous Monk,
I may have missed it but I don't think anyone who has replied so far has pointed out that what you have really isn't a traditional dispatch table. Have a look at Implementing Dispatch Tables. In your approach, all dispatches have to be integer based (array) where as with a hash, you have the luxury of using any scalar value.

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Subroutines in Dispatch Tables
by ikegami (Patriarch) on Sep 22, 2009 at 23:41 UTC

    In your approach, all dispatches have to be integer based (array) where as with a hash, you have the luxury of using any scalar value.

    The last bit should be "any string", not "any scalar value".

    And in his approach, all dispatches have to be regex-based, and that's rather more luxurious than any constant string.

    The cost is in speed. His dispatch takes O(N) to find the correct dispatch, not O(1).

    my $action; for my $dispatch (@$dispatch_tables) { my $regex = $dispatch->{regex}; if ($input =~ $regex) { $action = $dispatch->{action}; last; } } die("No match\n") if !$action; $action->($input);
      ikegami,
      Good catch on s/scalar/string/. I meant to add a line indicating that you could actually use just about anything you wanted as a hash key but that it would end up being stringified and unlikely do what you wanted.

      Regarding the luxury of using regexes - if your problem space fits a very specific model, you can still avoid the O(N) by using the technique outlined at Re: Massive regexp search and replace. With this particular implementation, I originally assumed that the idea was to loop over the regexes as you indicate with O(N) but then the usage in the example implied they index would be known. I probably read too much into it. Update: I should have said - either you are limited to integer dispatch values for O(1) or you will have to loop over the entire list O(N).

      Cheers - L~R

Re^2: Subroutines in Dispatch Tables
by Anonymous Monk on Sep 22, 2009 at 23:47 UTC
    Thanks for the link. I was doing a super search on dispatch table and found this link.

    This was quite close to what I wanted. A regex pattern with an associated action. And I read the link above and people were giving positive reviews on how this 'type' of dispatch table is being used.