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();

In reply to Moose log string class by jabowery

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.