Re: attr initializers (Class::MOP and Moose)

[email protected] (Stevan Little)
Newsgroups perl.moose
Message-ID <[email protected]>
Ricardo,

As mentioned in #moose, I very much like this idea. I do have a few  
questions ...

On Feb 6, 2008, at 12:18 PM, Ricardo SIGNES wrote:
> The initializer may be a coderef or a method name (referring to a  
> method on the
> instance.)  It's called like this:
>
>   $instance->$initializer($value, $name, $callback);

What is $name for again?

> The callback, when called, will set the instance slot to the passed- 
> in value.
> It makes it easy to have your initializer do the "hard" work  
> without having to
> worry about going through the several chains of methods needed to  
> set a slot
> value.  The example in the tests should look something like this:
>
>   package Example;
>   use Moose;
>
>   has foo => (
>     is => 'rw',
>     initializer => sub {
>       my ($self, $value, $name, $callback) = @_;
>       $callback->($value * 2);
>     }
>   );

I wonder if callback could be optional? And if it is not called, then  
it will just use the return value of the initializer sub?

> I think, but have not tested, that this will also work:
>
>   package Example;
>   use Moose;
>
>   has foo => (
>     is => 'rw',
>     setter      => 'set_foo',
>     initializer => 'set_foo',
>   );

What exactly would that be useful for? I am not sure I understand  
without seeing an example set_foo.

- Stevan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.