develooper Front page | perl.moose | Postings from December 2017

Re: Alternative to triggers?

Thread Previous
From:
Nick Perez
Date:
December 2, 2017 20:55
Subject:
Re: Alternative to triggers?
Message ID:
20171202215503.48e8a8f9@nicklaptop.localdomain
On Fri, 01 Dec 2017 19:07:01 -0700
Larry Leszczynski <larryl@emailplus.org> wrote:

> Hi all -
> 
> I have triggers set up on two attributes that check if their values
> are consistent with each other.  I'm worried that since triggers get
> called during construction when the values are passed in, it will try
> to do the check before both of those attributes have been set.  I
> kinda only want to do the check inside BUILD (at which point I know
> everything has been set), or later on when one or the other of those
> attributes changes.
> 
> I know how to call my checking code both from inside BUILD and as a
> trigger, what I don't know is how to make the trigger *not* fire
> during the initial construction... which makes me wonder whether
> there is a different/better way to do it without using triggers.
> 
> For purposes of discussion assume I have an attribute "color" which
> can be red/blue/green, and another attribute "size" which can be
> small/medium/large, and that any combination of color and size is ok
> except I want to croak if you try to use color "red" with size
> "small". 
> 
> 
> Thanks!
> Larry

Howdy Larry L.,

Take a look at some old blog posts from nothingmuch:
http://blog.woobling.org/2009/08/moose-triggers.html

In a nutshell: consider doing the check only during BUILD, avoid
mutable state, make a separate class to contain those attributes and
rebuild it as needed, or make the attributes private and provide a more
sophisticated method for altering them that does the check explicitly.

HTH,
nperez

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About