Since you're eval()ing the code they enter to set the variables and such, you can directly reproduce the code of the subroutine, as long as you request that they enter ONE subroutine at a time, and that subroutines are entered by themselves. This also does not work for code references. But it is a start.
# in evaluate()
if (/^\s*sub\s+(\S+)/) {
$WorkSpace::functions{$1} = $_;
}
Thanks... that is a good idea. I think what I might do is this:
Have a @code_lines array keep track of each line of code as the user enters it. This would allow me to implement a save command where the user can save their code to a file for later use. I'd need to pop values off of @code_lines for every undo command.
Have a new subs command that retrieves everything that looks like a subroutine definition from @code_lines and prints out their contents.