Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Exporting constants from a Moo role

by anonymized user 468275 (Curate)
on Aug 02, 2018 at 12:08 UTC ( [id://1219698]=note: print w/replies, xml ) Need Help??


in reply to Exporting constants from a Moo role

One of the features of Moo (and Moose) is to define attributes as readonly with a default value. So it seems only natural that the authors of Moose and Moo would expect such values to be exported as readonly accessors instead of explicitly using constants.

Update: For example:-

package MyRole; use Moo::Role; use strictures 2; has 'INDEX_FOO'=> ( is => 'ro', isa => 'Int', default => 42 );

One world, one people

Replies are listed 'Best First'.
Re^2: Exporting constants from a Moo role
by tobyink (Canon) on Aug 02, 2018 at 16:00 UTC

    You'd probably also want to prevent people from overriding the constant using:

    my $obj = MyClass->new(INDEX_FOO => 43);

    To do this, use:

    has 'INDEX_FOO'=> ( is => 'ro', default => 42 init_arg => undef, );

    But by that point, you have to ask, why not just do this?

    sub INDEX_FOO () { 42 }

      Thanks Toby! init_arg => undef is a new one on me. Why do you need to declare a default value (or what is the point) when you use that parameter to the attribute?

      I quite often use simple subs for constants if only because the syntax to declare them is simpler and shorter. I guess the author of this code (which is used to access the elements of a work queue job) prioritized simpler and shorter syntax at the calling end, preferring $self->job->[INDEX_FOO] to $self->job->[ $self->INDEX_FOO ] ...

      I think I like the solution you showed in your other reply. Thanks again!


      The way forward always starts with a minimal test.

        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.)

Re^2: Exporting constants from a Moo role
by 1nickt (Canon) on Aug 02, 2018 at 21:26 UTC

    Thank you, I quite agree. This is inherited code and I wanted to understand the limitations before changing it.


    The way forward always starts with a minimal test.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1219698]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 06:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found