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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.