BEGIN {
-package Ref::Tracer;
+package Visitor::NameTracking;
-use Moose;
+use Moose::Role;
use B qw(perlstring);
-use Scalar::Util qw(weaken refaddr);
use namespace::clean -except => 'meta';
-extends 'Data::Visitor';
-
-has '_traced_refs' => (is => 'ro', lazy => 1, default => sub { {} });
-has '_traced_names' => (is => 'ro', lazy => 1, default => sub { {} });
has '_current_trace_name' => (is => 'ro');
has 'root_name' => (is => 'ro');
-before visit_ref => sub {
- my ($self, $data) = @_;
-
- # can't just rely on refaddr because it may get re-used if the data goes
- # out of scope (we could play clever games with free magic on the wizard
- # or whatever but KISS) - but we -can- keep a weak reference which will
- # turn to undef if the variable disappears
-
- weaken($self->_traced_refs->{refaddr $data} = $data);
-
- $self->_traced_names->{refaddr $data} = $self->_current_trace_name;
-};
-
around visit => sub {
my ($orig, $self) = (shift, shift);
local $self->{_current_trace_name}
return $self->$orig(@_);
};
+package Ref::Tracer;
+
+use Moose;
+use Scalar::Util qw(weaken refaddr);
+use namespace::clean -except => 'meta';
+
+extends 'Data::Visitor';
+
+with 'Visitor::NameTracking';
+
+has '_traced_refs' => (is => 'ro', lazy => 1, default => sub { {} });
+has '_traced_names' => (is => 'ro', lazy => 1, default => sub { {} });
+
+before visit_ref => sub {
+ my ($self, $data) = @_;
+
+ # can't just rely on refaddr because it may get re-used if the data goes
+ # out of scope (we could play clever games with free magic on the wizard
+ # or whatever but KISS) - but we -can- keep a weak reference which will
+ # turn to undef if the variable disappears
+
+ weaken($self->_traced_refs->{refaddr $data} = $data);
+
+ $self->_traced_names->{refaddr $data} = $self->_current_trace_name;
+};
+
sub traced_ref_map {
my $self = shift;
my $refs = $self->_traced_refs;
sub visit_object { shift->visit_ref(@_) }
-#package Ref::Replacer;
+package Ref::Replacer;
+
+# note: we actually handle weaken as well as external refs because I intend
+# to use Data::Dumper as a first pass and YAML::XS as a second and neither
+# of them know how to deal with weak references
+#
+# I'm faintly curious to see if manual cross-ref-ification nad JSON::XS will
+# actually be faster for reconstructing structures but it's fairly academic
+# as yet
#use Moose;
#use Variable::Magic qw(getdata);