in reply to Re^3: Exporting constants from a Moo role
in thread Exporting constants from a Moo role
init_arg is used to tell Moo what key will get passed to the constructor for that attribute.
use v5.16; package Person { use Moo; has "nick-name" => ( is => "ro", init_arg => "nick", reader => "nick_name", ); }; my $robert = Person->new(nick => "Bob"); # init_arg say $robert->nick_name; # reader say $robert->{"nick-name"}; # attribute name (direct +hashref access)
Setting init_arg to undef means that the attribute cannot be passed to the constructor at all. (And if the attribute is also read-only, so there's no writer method, you should probably be setting a default for the attribute too!)
(And this is not Moo-specific. Moose and Mouse have the exact same features.)
|
|---|