On 6/22/06, David Landgren <[EMAIL PROTECTED]> wrote:
Could Perl get Reversible Debugging?
[...]
We need a "come from" instruction
http://xrl.us/nnuw
I don't recall reading a demand for a "come from" instruction in that thread,
but I had an idea last night that I was going to dismiss as trivial,
until reading
that assertion in the week in p5p summary. It's a real simply come from
instruction that forks at the labels instead of branching. Not suitable at all
as a replacement for goto -- if you want to branch, use goto. But may be
useful for setting checkpoints/breakpoints with a minimum of surgical insult.
package ComeFrom;
use Exporter;
@EXPORT_OK = qw/CFL ComeFromLabel CFlabel/, # all the same thing
qw/ComeFrom CF/; # also all the same thing
our %SubsByLabel;
sub ComeFrom($\&){
my ($label, $sub) = @_;
push @{$SubsByLabel{$label}}, $sub;
};
sub ComeFromLabel($;){
my $label = shift;
foreach my $sub (@{$SubsByLabel{$label}}){
unless(fork()){ #fixme: check for -1; install sane
SIG_CHLD handler, etc
goto &$sub; # keeping what's left of @_ after
shifting off the label
exit; # we're not trying to make fork bombs here
}
}
};
# establish more than one way to do it:
*CFL = *CFlabel = \&ComeFromLabel;
*CF = \&ComeFrom;
=head1 USAGE
Register subroutines with the ComeFrom system with the
ComeFrom::ComeFrom instruction
like so:
ComeFrom Jellybean => sub {
print "At ",~~localtime(),
"the bean count was ", CountBeans(),"\n"
};
Later this label can be jumped to -- this subroutine dispatched, or called, but
in its own process (that's the crucial difference between this mechanism and
a simple subroutine call -- subtle semantics) by inserting a 'JellyBean' CFlabel
into some code that may be affecting the bean count:
...
CFL 'Jellybean';
...
=head2 Differences Between ComeFromLabel and normal subroutine call
* ComeFrom execution happens in own process
* Multiple ComeFrom subroutines can be associated with a given label
* CFL markers can be trivially edited to #CFL no-ops after debugging
=head2 Why Bother?
This functionality is a trivial subroutine redispatcher. In the event
that it is easier
for you to write your own than install mine, please don't let me stop you.
=cut
__END__
So.. shall I put this on CPAN?
--
David L Nicol
"if life were like Opera, this would probably
have poison in it" -- Lyric Opera promotional
coffee cup sleeve from Latte Land