ropey has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks
I am sure many of you use the Class::Accessor module in one way or another, I have a little issue
I need to be able to dump a object to a hash, so each accessor created by Class::Accessor I want a key in the hash... hope that makes sense. Issue is I am not sure how to go about getting this data ? I am sure its there in plain sight but I cant seem to work it out....The code which creates the accessor is..
sub mk_accessors { my($self, @fields) = @_; $self->_mk_accessors('rw', @fields); } { no strict 'refs'; sub _mk_accessors { my($self, $access, @fields) = @_; my $class = ref $self || $self; my $ra = $access eq 'rw' || $access eq 'ro'; my $wa = $access eq 'rw' || $access eq 'wo'; foreach my $field (@fields) { my $accessor_name = $self->accessor_name_for($field); my $mutator_name = $self->mutator_name_for($field); if( $accessor_name eq 'DESTROY' or $mutator_name eq 'DESTR +OY' ) { $self->_carp("Having a data accessor named DESTROY in + '$class' is unwise."); } if ($accessor_name eq $mutator_name) { my $accessor; if ($ra && $wa) { $accessor = $self->make_accessor($field); } elsif ($ra) { $accessor = $self->make_ro_accessor($field); } else { $accessor = $self->make_wo_accessor($field); } unless (defined &{"${class}::$accessor_name"}) { *{"${class}::$accessor_name"} = $accessor; } if ($accessor_name eq $field) { # the old behaviour my $alias = "_${field}_accessor"; *{"${class}::$alias"} = $accessor unless defined & +{"${class}::$alias"}; } } else { if ($ra and not defined &{"${class}::$accessor_name"}) + { *{"${class}::$accessor_name"} = $self->make_ro_acc +essor($field); } if ($wa and not defined &{"${class}::$mutator_name"}) +{ *{"${class}::$mutator_name"} = $self->make_wo_acce +ssor($field); } } } }
Any ideas would be welcome ?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inside Class::Accessor
by perrin (Chancellor) on Feb 12, 2008 at 15:12 UTC | |
by ropey (Hermit) on Feb 12, 2008 at 15:29 UTC | |
by perrin (Chancellor) on Feb 12, 2008 at 18:08 UTC | |
by ropey (Hermit) on Feb 13, 2008 at 09:19 UTC | |
by perrin (Chancellor) on Feb 13, 2008 at 14:59 UTC | |
| |
by stiller (Friar) on Feb 12, 2008 at 19:27 UTC |