Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: NonMoose and (clean) inheritance

by eco (Acolyte)
on Mar 20, 2014 at 21:08 UTC ( [id://1079141]=note: print w/replies, xml ) Need Help??


in reply to Re: NonMoose and (clean) inheritance
in thread NonMoose and (clean) inheritance

Thanks for your answer! My preferred declaration of ClassB is as follows:

package ClassB; use Moose; extends 'ClassA'; has '+param1' => (default => 'PARAM1-CLASSB'); has '+param2' => (default => 'PARAM2-CLASSB');

But since this doesn't work for the param1, I figured to use FOREIGNBUILDARGS and BUILDARGS to have some similarity in usage. Your builders solution seems to do this better though. So thank you very much for the idea!

But if I could change ClassA to allow above syntax for ClassB that would be awesome

-- ecocode

Replies are listed 'Best First'.
Re^3: NonMoose and (clean) inheritance
by tobyink (Canon) on Mar 20, 2014 at 22:54 UTC

    Builders are just generally a better idea than defaults. (MooseX::Manual::BestPractices even tells you so.) They give you a nice clean way to override the behaviour in subclasses.

    If you use Moo or MooseX::AttributeShortcuts then you can even do:

    has foo => (is => 'ro', builder => sub { ... });

    ... and it will automatically get "translated" to:

    has foo => (is => 'ro', builder => '_build_foo'); sub _build_foo { ... }

    ... so builders can be just as convenient as defaults are.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

      Thanks for the link of Moose best practices. I didn't know about that one! It's true that builders are preferred over defaults, but it also specifies "However, do use a default when the default is a non-reference.." which happens to be the case here. But hence, for now your solution with builders is the best I have...

      -- ecocode

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found