RE: attribute initialisation and after

[email protected] (Jeff Hallock)
Newsgroups perl.moose
Message-ID <5995603F96DF2B4E80C77ED3E77AE2D05FB25B3E@CH1PRD0811MB408.namprd08.prod.outlook.com>
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 ) = @_;
		...
	}
);

- Jeff

-----Original Message-----
From: Dave Howorth [mailto:[email protected]] 
Sent: Friday, October 05, 2012 10:24 AM
To: [email protected]
Subject: attribute initialisation and after

I wrote a Moose class and the class 'has' an attribute. The attribute also has an 'after' modifier.

But when I create an object of the class and supply an initial value for the attribute to the 'new' method, it appears that the 'after' modifier is not called. It is called if I later access the attribute.

My expectation is that initialisation of an attribute would invoke the attribute's setter and the modifier, so reality doesn't match my expectation - is there something that explains why reality is as it is so I can adjust my expectation?

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.