On Wed, Oct 06, 2010 at 10:41:33PM +0200, Jiří Pavlovský wrote:
> On 6.10.2010 22:20, Ricardo Signes wrote:
>> * Jiří Pavlovský<[email protected]> [2010-10-06T15:50:13]
>>> is there some established opinion/best practice on how to provide
>>> some sort of startup/shutdown hooks to your Moose class?
>>> What I basically want is something like:
>>>
>>> package MyClass;
>>> use TheGreatClass;
>>> sub onStart { ... }
>>> sub onEnd { ... }
>>>
>>> in application:
>>>
>>> use MyClass;
>>> MyClass->new->run;
>>>
>>> Something akin to what Test::Class does, but possibly without the
>>> sub attributes.
>> When are onStart and onEnd run? before and after "run"? If so, you want to
>> write a "run" method and then add
>>
>> before run => sub { ... };
>>
>> after run => sub { ... };
>>
> Hello,
> thanks for the answers Ricardo and Jesse.
> I know about build/after as well as about BUILD/DEMOLISH. Still I need
> something little bit different. My example was not clear and has a
> mistake actually.
> I'll try to improve it here.
>
> So my class is 'TheGreatClass'. Now the user can derive from it and
> specify his hooks. There could be several methods designated to run
> 'before' and 'after' the 'run' and they would be run in say alphabetical
> order.
> I could probably craft something together, but wanted to ask first if
> there is some MooseX module that already does this or something similar.
>
> To invoke the example of 'Test::Class' again, there you can do:
>
> sub do_stuff : Test(startup) { }
>
> sub another_stuff_to_do : Test(startup) { }
>
> sub cleanup_stuff : Test(shutdown) { }
This is what Ricardo said - you want to be doing something like:
package TheGreatClass;
use Moose;
sub run {
# do stuff here
}
package MyClass;
use Moose;
extends 'TheGreatClass';
before run => sub {
# do_stuff
};
before run => sub {
# another_stuff_to_do
};
after run => sub {
# cleanup_stuff
};
-doy