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

    ... jabowery ... Please advise.

    a) perltidy

    b) word explanation of problem, question, ahead of "color

    c) check the cookbook

    I suspect there must be a way to define an attribute called "log_string" with a "reader" and a "writer" rather than the method "log_string".

    Yes, its called an object, so log_string is an object with a reader/writer/anythingyouwant

    I suspect there must be a way to use the class's list @log_string_parts rather than explicitly listing sequences of attribute accessors

    Yeah, my $method = $obj->can('method'); $obj->$method; ... see Modern Perl a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.

      Regarding:

      a) Thanks.

      b) I did not understand what you said.

      c) I found nothing in the cookbook about explicit "reader" and "writer" attribute modifiers as described in the Moose Manual about Attributes

      For example, I'd like to do something like this (which does not work with Moose even though one is supposed to be able to explicitly specify "reader" and "writer" accessors by name):
      package QuackLog; use Moose; use namespace::autoclean; use strict; my +@log_string_parts = qw(timestamp foo bar baz qux norf quack honk woof + meow blerf); has $_=> (is => 'rw') for(@log_string_parts); has log_s +tring => ( is => 'rw', reader=> 'get_log_string', writer => 'set_log_ +string', ); sub get_log_string{ my ($self) = shift; return(join ' ',m +ap {$self->can($_);$self->$_} @log_string_parts); } sub set_log_strin +g{ my ($self,$s)=@_; my @part_values=split / /,$s; for my $method (@l +og_string_parts){ $self->can($method); $self->$method(shift @part_val +ues); } $self; } __PACKAGE__->meta->make_immutable();

      This, however, produces a compile time error:

      You are overwriting a locally defined method (get_log_string) with an +accessor...

      PS: I did try the symbolic $method lookup, having used it many times before in Perl objects, but for some reason it didn't work, and I attributed the failure to my lack of understanding of Moose rather than the more likely explanation which was just a bad choice of syntax.

        b) word explanation of problem, question, ahead of "color b) I did not understand what you said.

        Instead of "humbleness" like "Being a mere calf...", use more words to explain the conext/programming problem

        Instead of having us guess what you think get_log_string and set_log_string are supposed to do, explain what they're supposed to do

        When you were talking about a reader/writer, it wasn't clear you were talking about Moose has() options , see below

        Also, you only show a class declaration, but no use case

        For example, I'd like to do something like this (which does not work with Moose even though one is supposed to be able to explicitly specify "reader" and "writer" accessors by name): You are overwriting a locally defined method (get_log_string) with an accessor...

        Did you know has() creates subroutines? So that you can write the following without writing a sub reader ... and sub write ...?

        my $val = $obj->reader; $obj->write( $newval);

        So, if has() is creating subroutines (getter/setter), to change some attribute ...

        what are your get_log_string and set_long_string supposed to do?

        Are you supposed to have a "has log_string"?

        This brings up check the cookbook again... there is stuff in there

        So i come up with

        So when an attribute is updated the log_string isn't, so back to cookbook

        Does this do what you want? Maybe

        Is it a good idea , good OOP/OOD? Maybe

        Does Moose teach you good OOP/OOD? No :)