Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
file.txt:
set 5 add 2 mul 2
interp.pl:
!/usr/bin/perl use strict; sub set { my ($var, $param) = @_; $var = $param; return $var; } sub add { my ($var, $param) = @_; $var = ($var + $param); return $var; } sub mul { my ($var, $param) = @_; $var = ($var * $param); return $var; } my $register = 0; while( <STDIN> ) { my ($command, $param) = split " ", $_; print $command, "->", $param, "\n"; if( $command eq 'set' ) { $register = set( $register, $param ); } elsif( $command eq 'add' ) { $register = add( $register, $param ); } elsif( $command eq 'mul' ) { $register = mul( $register, $param ); } else { die "Unknown operator: $command\n"; } print "currently: $register\n"; print "------\n"; }
I'm not *actually* trying to write an asm interpreter, but it serves as a good example of where I want to take a reasonably complicated action based on data that's outside of my control (so please don't say "eval( <perl> )" right away ;^). The functions that I'm calling, I'll want to put in a separate module (ie: ops.pm) to make maintenance easier, but that's something for the future. Please feel free to suggest creative ideas as well, as I'm kindof stuck.
Thanks!
--Robert
|
---|