>>>>> "Rafael" == Rafael Garcia-Suarez <[EMAIL PROTECTED]> writes:
Rafael> What do you expect B::Deparse::coderef2text to do here ? Output
Rafael> something equivalent to { 42 + shift } ?
Yes. Because that would be a serialization of the current "behavior".
When you dump:
@foo = qw(a b c);
you're not dumping a 3 element array with undef for the values. You're
dumping its *current* state. Similarly, coderef2text should dump
something like:
{ 42 + shift }
or ideally
do { my $constant = 42; sub { $constant + shift } }
which captures the current state of the world.
Think of
my $constant = ...;
sub { $constant + shift }
as a class. A generator. I don't want to dump a class. I want
to dump a specific *instance*. Put another way:
sub black_adder {
my $constant = shift;
sub { $constant + shift }
}
my $ten = black_adder(10); my $twenty = black_adder(20);
I would expect the text of code2text($ten) to be *different* from the
text of code2text($twenty). After all, they refer to a different
contextual variable, and that hidden state (10 vs 20) is important to
the serialization and later thawing of the code. It *changes the
behavior*.
Here's another example:
sub inky {
my $cur = 0;
sub { ++$cur; }
}
my $x = inky(); my $y = inky(); # two different closures
$x(); $x(); $x(); # bump to 3
$y(); $y(); # bump to 2
Again, I'd like to dump $x so I can restore it later. The only
way to do that is to somehow include a literal 3 in the text.
Similarly, dumping $y has to have a 2 somewhere.
Now, if that looks easy, let's go one step further, from an example
from my PROM class:
sub hidden {
my $shared = shift;
return sub { $shared }, # getter
sub { $shared = shift }; # setter
}
my ($x_get, $x_set) = hidden(0);
my ($y_get, $y_set) = hidden(99);
Now, if I dump $x_get and $x_set, I expect it to create text that
shows that a hidden variable connects them. That's what will be
needed to properly restore it. Similarly, if I dump all four
subroutines, I'll have to have two hidden variables, present to the
numbers 0 and 99 (unless I've called the setter sometime later).
There.
That's what I expect code2text to do. Dump the coderef so that I can
restore it. And for closures, it must dump the state of the closure
variables. It has to do that, so that I can restore the subroutine to
its current state, just like when I dump a variable to be able to
restore the variable to its current state. That's what marshalling
means.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!