Re: Moose n00b question about writer / reader
[email protected] (Buddy Burden) Sun, 19 Apr 2015 17:37:50 -0700
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
Andrew, >> I want to create a class that has some attributes. However, there’s >> some logic that needs to be executed when setting values on these >> attributes. In some cases, I’ll need to throw an error like “you can’t >> set that value on this object attribute”, I also need to be able to >> modify the value on the way into the object sort of like a DECODE() >> statement in SQL, etc. > You'll want to scroll half a dozen screenfuls downward on the same page > and check out the section titled "Triggers". These can be used to do > exactly the things you want. > > Setting writer / reader, on the other hand, is just providing names to > the accessor routines which Moose creates automatically for you. Harald is absolutely correct. I don't know if this will help or not, but here's how I think of it. In Moose, just as in C++,[1] there's a distinction between initialization and assignment. When you call a constructor, you're initializing the attributes. When you call a mutator,[2] you're assigning to an attribute. A trigger is a way to hook into to both methods of setting the attribute.[3] And, if that _doesn't_ help, feel free to ignore me. :-) -- Buddy [1] And maybe in other OO languages as well (Java, perhaps?). [2] What we generally call an accessor. But, if your attribute is rw, then it's technically a mutator. [3] C++ doesn't have such a thing. Yet another reason I prefer Moose. :-)