develooper Front page | perl.beginners | Postings from April 2010

Dispatch table using Tie::RegexpHash

Thread Next
Steve Bertrand
April 29, 2010 17:30
Dispatch table using Tie::RegexpHash
Message ID:
Hi all,

Given the threads on dispatch tables recently, I was writing a bit of a
whitepaper on how dts are useful for consolidating code.

I've run into an issue that I've been working on for some time (while
also performing my job function ;), and I'm hoping that extra eyeballs
will notice the problems.

Before I get elaborate in any which way with my documentation, I've just
been using a basic example (that you likely won't need to follow): (and dt2.* and dt3.*)

Here is what I have that is breaking and I can't figure out why:


use warnings;
use strict;

use Tie::RegexpHash;

my $input   = $ARGV[0];

my $number  = qr/^\d+$/;
my $alpha   = qr/^\w+$/;

tie my %dt, 'Tie::RegexpHash';

%dt = (

        common  => sub {
                    my $param = shift;
                    print "$param\n";

        $number => sub {
                    return int( rand( 3 ) +1 );

        $alpha  => sub {
                    my @letters = ('a'..'z');
                    return $letters[ rand scalar @letters ];

        run     => \&run,

sub run {

    my $param   = shift;

    $dt{ common }( $param ) ;
    $dt{ run }( $dt{ $param } );

$dt{ run }( $input );


... I've never used Tie::RegexpHash before, so if you can see what I was
trying to do with it, I'm open to criticism.



Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About