Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^9: Moose and default values

by morgon (Curate)
on Feb 20, 2013 at 23:28 UTC ( #1019864=note: print w/replies, xml ) Need Help??


in reply to Re^8: Moose and default values
in thread Moose and default values

Were getting here :-)

Ideally I would want to declare everything at the attriute-level, e.g.:

has foo => (is => "ro", default => { value => "whatever", when => "fal +se" });
Can you do that as well?

I am not so much interested in the feature itself, but it is interesting to see some examples of Moose-metaprogramming at work...

Thanks!

Replies are listed 'Best First'.
Re^10: Moose and default values
by tobyink (Abbot) on Feb 20, 2013 at 23:54 UTC

    In this case what I'd suggest is a custom type constraint.

    use Moose::Util::TypeConstraints; subtype 'TrueStr', as 'Str', where { !!$_ }; coerce 'TrueStr', from 'Any', via { $_ || "whatever" }; has foo => (is => "ro", isa => "TrueStr", coerce => 1);

    Then any value which is not a TrueStr will be coerced into one via the code $_ || "whatever".

    Of course, this also applies to accessors, not just the constructor. (Not a problem for a read-only attribute though!) And also, it means that if you've got a bunch of attributes like this, each with different defaults, you need to define a type constraint for each one.

    The alternative - attribute metaprogramming - starts off seeming simple enough, but becomes more complicated when you start thinking about immutability. (Attribute metaprogramming can be a real bitch when it touches on immutability.)

    A compromise would be to hook has to automatically build per-attribute type constraints and coecions. I submitted a patch to MooseX::AttributeShortcuts for precisely this feature some months ago, but it was only partly accepted: per-attribute constraints are now supported, but not coercions.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019864]
help
Chatterbox?
[robby_dobby]: This anonymonk's post reads weird to me
[robby_dobby]: Other than the content, that Chrome would not allow submitting code sounds like something funky going on there :P
[erix]: lazyness-driven inventio :)
[erix]: "the dog ate my homework"
[Eily]: looks more like "all my plates are broken and I don't understand why. Also I can't retrieve the broken parts because the elephant in the room is on the way"
[ambrus]: Eily: also webpages aren't displayed on my monitor no matter what I click with the mouse, and there's a blackout right now, could that be a problem?
[robby_dobby]: ambrus: Yes
[hippo]: erix++: "the browser ate my code"
[robby_dobby]: Eily: Now, people are trying to be inventive with crowdsourcing their work, eh? Sure, call on the admins to k-line these sockpuppet accounts!
[robby_dobby]: hippo: I like how you equated your browser to a dog....

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2017-04-25 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (453 votes). Check out past polls.