Firstly use constant doesn't define its constants with the sub name in the correct package. Moo::Role knows they were compiled into the constant:: package, not into your role, so doesn't copy them to your class.
Secondly, with is operating at run-time, not compile time.
You can solve these by using PerlX::Define to define your constants, and wrapping with with BEGIN { ... }.
use v5.16; package MyRole { use Moo::Role; use PerlX::Define; define BLAH = 42; } package MyClass { use Moo; BEGIN { with 'MyRole' }; sub do_stuff { return BLAH } } say MyClass->new->do_stuff;
Or (same idea) just manually do the constant sub yourself:
use v5.16; package MyRole { use Moo::Role; sub BLAH () { 42 } } package MyClass { use Moo; BEGIN { with 'MyRole' }; sub do_stuff { return BLAH } } say MyClass->new->do_stuff;
In reply to Re: Exporting constants from a Moo role
by tobyink
in thread Exporting constants from a Moo role
by 1nickt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |