artist has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
In many applications I see 'Undo' feature. For example Microsoft Word or Photoshop or even Emacs editor. How I can achieve the similar effect in Perl? For example see the following code.
#!/usr/local/bin/perl
use Data::Dumper;
my $hash = { a => 21, b => 32, c => 43 };
my $array = [ 11,12,13,14];
foreach (1..3){
perform_operationX();
print_data();
}
cancel_last_operationX(2);
sub perform_operationX {
my $letter = chr(97+ int rand(10));
my $number = int rand(100);
$hash->{$letter} = $number;
shift @$array;
}
sub print_data{
print Dumper($hash,$array);
}
sub cancel_last_operationX {
my $number_of_operations = shift;
......... UNDO CODE .............
}
In order to achieve this if we have to save the state of the previous operation how do we do it?
An reading/code/material/module etc would be useful.
Thank You
{Artist}
Re: Undo Operations
by Corion (Patriarch) on Nov 14, 2003 at 16:36 UTC
|
The only discussion I have seen of this feature is the Gang of Four book on Design Patterns. They call it the "Action Pattern" there, and the idea is quite simple :
In your program, you have (named) actions, that the user can execute, for example in Word, insert a character, insert a word, insert a range, delete a character, delete a word, delete a range. Cutting and pasting text can be modeled as deleting and inserting of several words and characters. Every Action is put into a list, which will be called the "undo list" for obvious reasons. Now, inverting an action is very simple for insert and delete actions, and if you take an object oriented approach, you can simply make it that every action knows how to undo itself.
These actions must be wedged inbetween the model and the controler if you have an MVC approach. If you don't have an MVC approach, I have no idea how you will find all the possible locations where you need to put special registering code.
Note that the undo list and the code needed for it also makes your application very easily scriptable, as a script is nothing more than a saved "do"-list - you just need a way to tell your actions whether they should do or undo themselves.
perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The
$d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider
($c = $d->accept())->get_request(); $c->send_response( new #in the
HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
| [reply] [d/l] |
Re: Undo Operations
by IlyaM (Parson) on Nov 14, 2003 at 16:36 UTC
|
Look for Command pattern on google. It should have some examples (probably non-perl though).
| [reply] |
Re: Undo Operations
by TomDLux (Vicar) on Nov 14, 2003 at 17:46 UTC
|
| [reply] |
Re: Undo Operations
by hmerrill (Friar) on Nov 14, 2003 at 16:47 UTC
|
If you need an 'undo' feature, the typical thought that comes to my mind is:
1. make a *before* copy of the data
2. let the user make changes to the data
3. if changes were made, ask the user if
he/she wants to keep those changes
4. if user wants to keep changes, move or
copy the new data to the *current* data
if user does not want to keep changes, move
or copy *before* data to *current* data
That's kind of a simplistic method, but I think it achieves the goal. Is that what you were looking for?
Hope this helps. | [reply] [d/l] |
|
|