SPUG: Data::Dumper for an entire package?
tuck at whistlingfish.net
Thu Jul 12 14:51:59 CDT 2001
-- Jason Simpson <jason at xio.com> spake thusly:
> I'd like to dump (and restore later) the state of all the variables
> in a particular package. Is there a way to do this?
My guess is that you're not actually asking the right question. The
right question would be, "Would it be a good idea to try to magically
dump all the variables from a package which I could then later restore
and hope everything worked?"
I would say the answer short answer to that would be "No," and the long
answer would be "If you're looking for persistent behavior from a
package, the package should be written in such a way that it provides
that behavior as part of the design."
However, if you really want to do such a thing, you could start playing
around with the %otherpackage:: array (the trailing "::" is vital),
which would tell you what variable names are getting used. You would
then have to determine for each name whether the hash, array and scalar
were populated and dump each of them in turn.
As I recall, Apache::PerlRun has a flush_namespace function which does
some prowling through namespaces to a slightly different end. If you're
still bent on this mad path, you could look there for inspiration.
> I tried a few things along the lines of
> #!/usr/bin/perl -w
> use Data::Dumper;
> package otherpackage;
> $a = 1;
> @a = ('a', 'b');
> $b = 2;
> %b = ('key', 'value');
> $c = 3;
> package main;
> open DATA, ">otherpackage.data";
> $data = Dumper (\%otherpackage::);
> print $data;
> print DATA $data;
> close DATA;
> but from looking at the output file (otherpackage.data), that didn't
> do what I was hoping.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 232 bytes
Desc: not available
Url : http://mail.pm.org/archives/spug-list/attachments/20010712/29db92f5/attachment.bin
More information about the spug-list