jabowery has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to define an attribute called "log_string" with a "reader" and a "writer" as described in the Moose Manual about Attributes, but the following code produces an error You are overwriting a locally defined method (get_log_string) with an accessor...
The purpose of the "log_string" attribute is to provide an I/O representation of the object's data as a line in a log file. The internal representation of the object is a set of attributes, one per space separated text field in the line from the log.
Please advise as to if and how one can attach the reader and writer methods to the log_string attribute so that the usual syntax of $logged_event->log_string calls the "get_log_string" method and the $logged_event->log_string($string_from_log) calls the "set_log_string" method.
My current code simply has a "log_string" method rather than a "has" declaration. But is that really the Moose way to do this?
package QuackLog; use Moose; use namespace::autoclean; use strict; my @log_string_parts = qw(timestamp foo bar baz qux norf quack honk wo +of meow blerf); has $_=> (is => 'rw') for(@log_string_parts); has log_string => ( is => 'rw', reader=> 'get_log_string', writer => 'set_log_string', ); sub get_log_string{ my ($self) = shift; return(join ' ',map {$self->can($_);$self->$_} @log_string_par +ts); } sub set_log_string{ my ($self,$s)=@_; my @part_values=split / /,$s; for my $method (@log_string_parts){ $self->can($method); $self->$method(shift @part_values +); } $self; } __PACKAGE__->meta->make_immutable();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moose log string class
by Anonymous Monk on Feb 11, 2015 at 02:59 UTC | |
by jabowery (Beadle) on Feb 12, 2015 at 02:35 UTC | |
by Anonymous Monk on Feb 12, 2015 at 03:38 UTC |