in reply to Re: Inheritable configuration options.... but with default values?
in thread Inheritable configuration options.... but with default values?
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
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:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Inheritable configuration options.... but with default values?
by bliako (Abbot) on Feb 21, 2020 at 00:03 UTC | |
by LanX (Saint) on Feb 21, 2020 at 00:24 UTC | |
|
Re^3: Inheritable configuration options.... but with default values?
by LanX (Saint) on Feb 21, 2020 at 00:45 UTC |