Skip to content

Instantly share code, notes, and snippets.

@davorg
Created November 12, 2012 09:16
Show Gist options
  • Save davorg/4058307 to your computer and use it in GitHub Desktop.
Save davorg/4058307 to your computer and use it in GitHub Desktop.
Simple dispatch table example
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
# Define allowed operations
my %operation = (
'>' => sub { $_[0] > $_[1] },
'<' => sub { $_[0] < $_[1] },
);
sub operate {
my ($x, $op, $y) = @_;
if ($operation{$op}) {
return $operation{$op}->($x, $y);
} else {
die "$op is an unknown operation";
}
}
if (operate(1, '<', 2)) {
say "1 < 2";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment