wisemonkey has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I'm trying to use switch statement to create a file with hex opcodes for given mnemonics. I've code something like
switch ($opcode) { 'add' { print "\n The opcode reported was add with hex value 4 +"; } 'sub' { print "\n The opcode reported was sub with hex value 5 +"; } }
I tried double quotes around add and sub but it didn't help. The error seems to be:
syntax error at ./assembly2hex.pl line 21, near "'add' { " syntax error at ./assembly2hex.pl line 22, near "'sub' { "
Complete code until now is something like:
#!/usr/bin/perl use warnings; use diagnostics; use strict; use Switch; print ("Provide a file name to be used for source assembly code: "); my $assemblyFile = <STDIN>; print ("\n You provided a file : $assemblyFile "); open ASMFILE, $assemblyFile or die $!; while (my $instruction = <ASMFILE>) { print ("\n Current instruction is : $instruction "); my ($opcode, $operands) = split (/\s+/, $instruction, 2); print ("\n Instrction parts right now are $opcode \t $operands "); switch ($opcode) { 'add' { print "\n The opcode reported was add with hex value 4 +"; } 'sub' { print "\n The opcode reported was sub with hex value 5 +"; } } }
Any ideas? I've been reading about switch at http://perldoc.perl.org/Switch.html

Replies are listed 'Best First'.
Re: Switch statement giving out error
by cdarke (Prior) on Aug 26, 2011 at 07:40 UTC
    Don't use Switch, it is flaky and deprecated and no longer used. The Perl equivalent to C's switch statement is given.
Re: Switch statement giving out error
by Marshall (Canon) on Aug 28, 2011 at 16:42 UTC
    There is also another possibility besides "given", use a hash table. A simple translation like this could fit your needs:
    my %ops=(add=>4, sub=>5); Instead of writing "given" code, just do a hash look up. Often replacing code with a static data table lookup is a good idea.

    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