Search code examples
moose

Can Build function in moose return false?


I want to apply some roles dynamically to a moose class. I am writing that logic inside BUILD function. If there are any failures while applying role, I want to return FALSE. What will be the impact of that return value. How can I handle failure in the code where I am creating the object of this class?


Solution

  • Moose ignores the return value of BUILD; it is called for side-effects only. I'd expect "failures while applying role" to be caused by bugs and those failures to be evidenced by runtime exceptions. If you don't want the program to crash because of runtime exceptions, appropriately wrap your call to new() with eval.