I am reading a file and want to automatically generate accessors based on the fields in this file. I start out generating a small subset of accessors that I need to read status and error stuff, but then I want to add more accessors as I read the file. So far, I have not been able to accomplish this. No error messages are generated, but when I try to call an accessor on a dynamically generated accessor, I get the following error:
Can't locate object method "mymethod" via package "mypackage"
Here is my code where I pass a key, value pair from the file into a sub called kv that is supposed to generate methods:
package KLARF;
use strict;
use warnings;
use base qw(Class::Accessor);
# set up initial accessors
KLARF->mk_accessors( qw( state debug current_list ) );
# overload state accessor to print current state in debug mode
sub state {
my $self = shift;
if ( @_ ){
$self->{state} = shift;
print "$self->{state}\n" if $self->{debug};
return $self->{state};
}
else {
print "$self->{state}\n" if $self->{debug};
return $self->{state};
}
}
sub kv {
my $self = shift;
my $key = shift;
my $value = shift;
$key = lc $key;
if ( $self->{state} eq 'RECORD' ){
$value =~ s/\"//g;
chomp $value;
}
if ( $self->{state} eq 'FIELD' ){
$value =~ s/\"//g;
$value =~ s/\s//g;
my @values = split /,/, $value;
$value = \@values;
}
if ( $self->{debug} ){ print uc "Key is $key, Value is $value\
+n"};
$self->{$key} = $value;
KLARF->make_accessor( $key ); # this doesn't seem to work???
}
I have done this already using Moose, but I can't use Moose for this application. Maybe this is not even possible using Class::Accessor, I couldn't find anything saying it was or wasn't. Thanks for any help.