Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Moose log string class ( cached attribute trigger clearer )

by Anonymous Monk
on Feb 12, 2015 at 03:38 UTC ( #1116433=note: print w/replies, xml ) Need Help??


in reply to Re^2: Moose log string class
in thread Moose log string class

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1116433]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2018-12-17 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (72 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!