Re: attribute initialisation and after

[email protected] (Dave Howorth)
Newsgroups perl.moose
Organization MRC Laboratory for Molecular Biology
Message-ID <[email protected]>
Jeff Hallock wrote:
> Hi Dave,
> 
> 'after' is called after a method call.
> 
> If you want a block of code to be called every time you set an attribute, you want to use a trigger:
> 
> has 'foo' => (
> 	is => 'rw',
> 	trigger => sub {
> 		# will be called when setting the attribute via 'new' or via the writer method
> 		my ( $self, $newval, $oldval ) = @_;
> 		...
> 	}
> );

That's great. My code works now. Thanks Jeff.

It does seem counter-intuitive. I'd expect

  $o = Class->new(attribute => 'value');

to have the same effect as

  $o = Class->new(); $o->attribute('value');

Anyway, at least it works and hopefully I'll remember the exception for
next time.

Cheers, Dave
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.