It is also possible to have a hash table which contains code references to subroutines. This is called a dispatch table. I just show a simple example below. There are all sorts of permutations that can be done using this theme. Setting something like this up can be relatively hard or low performance in other languages but, not so in Perl.
Have fun!
#!/usr/bin/perl use warnings; use diagnostics; use strict; my %opcodes = ('add'=> sub{ print "the add opcode was passed operands: @_\n"; print "The opcode reported was add with hex value 4\ +n";}, 'sub'=> \&opcode_sub, ); while (my $instruction = <DATA>) { my ($opcode, @operands) = split (/\s+/, $instruction); print "\ninstruction: $opcode operands: @operands\n"; print "calling $opcode...\n"; $opcodes{$opcode}->(@operands); # give some data to subroutine #$opcodes{$opcode}->(); # or no args } sub opcode_sub { print "The code for subtract opcode was passed these operands: @_\n +"; print "The opcode reported was add with hex value 5\n"; } =outputs instruction: add operands: 5 6 10 calling add... the add opcode was passed operands: 5 6 10 The opcode reported was add with hex value 4 instruction: sub operands: 7 8 calling sub... The code for subtract opcode was passed these operands: 7 8 The opcode reported was add with hex value 5 =cut __DATA__ add 5 6 10 sub 7 8
In reply to Re: Switch statement giving out error
by Marshall
in thread Switch statement giving out error
by wisemonkey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |