in reply to Exporting constants from a Moo role
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;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Exporting constants from a Moo role
by 1nickt (Canon) on Aug 02, 2018 at 21:43 UTC |