in reply to Moose log string class

... 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.

Replies are listed 'Best First'.
Re^2: Moose log string class
by jabowery (Beadle) on Feb 12, 2015 at 02:35 UTC

    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 :)