Created
November 12, 2012 09:16
-
-
Save davorg/4058307 to your computer and use it in GitHub Desktop.
Simple dispatch table example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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