From: Matt S Trout Date: Sat, 20 Jun 2009 17:43:06 +0000 (-0400) Subject: role seperation for nomenclature elucidation X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=db1fc20b97ddb70c0b57d4b67c56d0a2ae67db38;p=gitmo%2FMooseX-Antlers.git role seperation for nomenclature elucidation --- diff --git a/tracer.pl b/tracer.pl index dda089b..4cee5c5 100644 --- a/tracer.pl +++ b/tracer.pl @@ -1,33 +1,15 @@ 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} @@ -57,6 +39,32 @@ around visit_scalar => sub { 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; @@ -76,7 +84,15 @@ sub traced_ref_map { 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);