I'm confused, in my book is new() reserved for object constructors not classes.

Inheritance for classes should be easier done by

use strict; use warnings; use Data::Dump qw/pp dd/; package Class1 { our %cfg = (a => 1, b=>2); sub cfg { %cfg } }; package Class2 { use parent -norequire, 'Class1'; our %cfg = ( %Class1::cfg, a=>11,c=>33 ); }; package Class3 { use parent -norequire, 'Class2'; our %cfg = ( %Class2::cfg, b=> 222 ); }; pp \%Class1::cfg, \%Class2::cfg, \%Class3::cfg; # **** THIS DOESN'T WORK package Class2b { use parent -norequire, 'Class1'; our %cfg = ( SUPER->cfg(), a=>11,c=>33 ); }; pp {Class1->cfg()}, \%Class2b::cfg;

Please note that I couldn't make it work with SUPER, ( but I'm no SUPER expert anyway ;-)

(
  { a => 1, b => 2 },
  { a => 11, b => 2, c => 33 },
  { a => 11, b => 222, c => 33 },
)
Can't locate object method "cfg" via package "SUPER" (perhaps you forgot to load "SUPER"?) at d:/tmp/pm/class_cfg.pl line 32.

Compilation exited abnormally with code 255 at Fri Feb 21 00:04:51

edit

ah I misread perlobj

> The SUPER modifier can only be used for method calls. You can't use it for regular subroutine calls or class methods:

update

This works, albeit with ugly syntax.

... package Class2b { use parent -norequire, 'Class1'; our %cfg = ( __PACKAGE__->SUPER::cfg(), a=>11,c=>33,n=>'2b' ); }; pp \%Class2b::cfg;

{ a => 11, b => 2, c => 33, n => "2b" }

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to Re^2: Inheritable configuration options.... but with default values? by LanX
in thread Inheritable configuration options.... but with default values? by Amblikai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.