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

Dispatch table using Tie::RegexpHash

Thread Next
From:
Steve Bertrand
Date:
April 29, 2010 17:30
Subject:
Dispatch table using Tie::RegexpHash
Message ID:
4BDA249C.1000701@ipv6canada.com
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):

http://ipv6canada.com/code/dispatch_tables/dt1.pl.txt (and dt2.* and dt3.*)

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

#!/usr/bin/perl

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 );

__END__

... 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.

Cheers,

Steve

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About