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
    So... what's the problem? Isn't $self a hash already?

      $self is, however I dont want all the keys which are in $self, I want the name of all of the accessors which are created for a particular class as well as its parent class

        Okay then, subclass the mk_accessors method in your class and stick the names of the fields passed to it into a hash. Then you can just dump that hash when you want it.
          A reply falls below the community's threshold of quality. You may see it by logging in.
        It is a bit hard to imagine why you want this? The accessor names are pretty predictible after all?