Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
use strict; { package Dispatch::Table; use overload '&{}' => sub { my $x=shift; sub { $x->lookup($_[0]) } }, '.' => sub { (shift)->lookup(shift) }, '~~' => sub { (shift)->exists(shift) }; sub new { my $class = shift; my @self; while (@_) { my ($test, $action) = (shift, shift); push @self, [$test => $action]; } bless \@self, $class; } sub lookup { my ($self, $value) = @_; foreach my $entry (@$self) { if ($value ~~ $entry->[0]) { return ref $entry->[1] eq 'CODE' ? $entry->[1]->($value) : $entry->[1]; } } return; } sub exists { my ($self, $value) = @_; foreach my $entry (@$self) { if ($value ~~ $entry->[0]) { return ref $entry->[1] eq 'CODE' ? $entry->[1] : sub { $entry->[1] }; } } return; } } sub action_1_to_999 { print "1 to 999\n"; } my $dispatch = Dispatch::Table->new( 0 => sub { print "Zero\n" }, [1..10] => sub { print "Single digit\n"}, 1_000 => sub { print "1e3\n" }, qr/^\d{4}/ => sub { print "Over a thousand\n"}, sub { my $x = shift; $x>0 && $x<1000 } => \&action_1_to_999, ); $dispatch.0; # call dispatch table on value '0' $dispatch.3; # call dispatch table on value '3' $dispatch.23; # guess! # call dispatch table on '999999' but only if the dispatch table # has an entry that covers value '-1'. $dispatch.999999 if $dispatch ~~ -1; # call dispatch table on '1000' but only if the dispatch table # has an entry that covers value '4'. $dispatch.1000 if $dispatch ~~ 4; __END__ Zero Single digit 1 to 999 1e3

In reply to Re: Using Number Ranges in a Dispatch Table by tobyink
in thread Using Number Ranges in a Dispatch Table by planetscape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-19 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found