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

#!/usr/bin/perl -- 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 qw/ log_string is rw isa Str lazy 1 builder _get_log_string /; sub _get_log_string { my ($self) = shift; my $str = join ' ', map {; my $str = $self->$_; length $str ? $str : '' } @log_string_parts; return $self->log_string( $str ); } 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 ); } return $self->_get_log_string; } __PACKAGE__->meta->make_immutable(); package main; use strict; use warnings; my $ql = QuackLog->new ; dd( empty => $ql ); $ql->qux('qux'); dd( single => $ql ); dd( single => $ql->log_string ); dd( double => $ql ); dd( full => $ql->set_log_string( q/timestamp foo bar baz qux norf quac +k honk woof meow blerf/ ) ); dd( full => $ql->log_string ); $ql->qux('XXXXXX'); ## uh oh, log_string not updated dd( full => $ql , $ql->log_string ); __END__ ("empty", bless({}, "QuackLog")) ("single", bless({ qux => "qux" }, "QuackLog")) ("single", " qux ") ( "double", bless({ log_string => " qux ", qux => "qux" }, "QuackLog"), ) ( "full", "timestamp foo bar baz qux norf quack honk woof meow blerf", ) ( "full", "timestamp foo bar baz qux norf quack honk woof meow blerf", ) ( "full", bless({ bar => "bar", baz => "baz", blerf => "blerf", foo => "foo", honk => "honk", log_string => "timestamp foo bar baz qux norf quack honk woof meow + blerf", meow => "meow", norf => "norf", quack => "quack", qux => "XXXXXX", timestamp => "timestamp", woof => "woof", }, "QuackLog"), "timestamp foo bar baz qux norf quack honk woof meow blerf", )

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

#!/usr/bin/perl -- 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); my $clearer = sub { $_[0]->clear_log_string }; has $_=> ( is => 'rw' , trigger => $clearer ) for @log_string_parts; has qw/ log_string is rw isa Str lazy 1 builder _get_log_string clea +rer clear_log_string /; sub _get_log_string { my ($self) = shift; my $str = join ' ', map {; my $str = $self->$_; length $str ? $str : '' } @log_string_parts; return $self->log_string( $str ); } 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 ); } return $self->_get_log_string; } __PACKAGE__->meta->make_immutable(); package main; use strict; use warnings; my $ql = QuackLog->new ; dd( empty => $ql ); $ql->qux('qux'); dd( single => $ql ); dd( single => $ql->log_string ); dd( double => $ql ); dd( full => $ql->set_log_string( q/timestamp foo bar baz qux norf quac +k honk woof meow blerf/ ) ); dd( full => $ql->log_string ); $ql->qux('XXXXXX'); ## uh oh, log_string not updated dd( full => $ql , $ql->log_string ); __END__ ("empty", bless({}, "QuackLog")) ("single", bless({ qux => "qux" }, "QuackLog")) ("single", " qux ") ( "double", bless({ log_string => " qux ", qux => "qux" }, "QuackLog"), ) ( "full", "timestamp foo bar baz qux norf quack honk woof meow blerf", ) ( "full", "timestamp foo bar baz qux norf quack honk woof meow blerf", ) ( "full", bless({ bar => "bar", baz => "baz", blerf => "blerf", foo => "foo", honk => "honk", log_string => "timestamp foo bar baz XXXXXX norf quack honk woof m +eow blerf", meow => "meow", norf => "norf", quack => "quack", qux => "XXXXXX", timestamp => "timestamp", woof => "woof", }, "QuackLog"), "timestamp foo bar baz XXXXXX norf quack honk woof meow blerf", )

Does this do what you want? Maybe

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

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


In reply to Re^3: Moose log string class ( cached attribute trigger clearer ) by Anonymous Monk
in thread 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.