On Oct 18, 2011, at 11:09 AM, Elliot Shank wrote: > On 10/18/11 10:02 AM, Peter Gordon wrote: >> I would like to be able to define package MyRole such that when it is >> invoked, runMe() gets run automatically without having to run >> it explicitly in package Y. > > This has nothing to do with Moose. It's the same for any module. You can do > something like > > > package Foo; > > runMe(); > > sub runMe { > ... > } # end runMe() > > 1; > > > When a module gets loaded, it first gets compiled and then the code in the > file scope gets run. This is how the module does its return of a true value: > the result of require is the last value returned by execution of the module, > which is traditionally handled by putting a "1;" at the end.
That would run only at compile time, and it would have to be invoked as __PACKAGE__->runMe() in the class to get at the meta class for the role (if the intent is to get at the attributes). Invoking runMe() in &BUILD will instead run per instance, so I guess however it's invoked depends on the need. chris