This is a small trick I like to use when defining callbacks. Instead of invoking callbacks as code references:
I always write the invocation as a method call:
or if $data is not a natural invocant for the routine, then I invoke it as a method on the object that is sending the notification:
This works in more cases than just plain code reference invocation:
- If $data is an object then $callback can be a string (a method name).
- If $callback is a code reference there is no difference between this syntax and code dereferncing
- Even if $data is not an object but $callback is a code reference, the method syntax will still work.
It's pretty nice to be able to pass method names as callbacks invoked on a subclass but still have the flexibility of a code reference when appropriate, and works especially well when the callback attribute has a sensible default value that resolves to a method call on $self to allow easy overriding of behavior without needing a full subclass.