Fellow monks showed you how to do it with Moo. I will show you how to do it without any OO system.

# Simple OO example with inheritance # author: bliako # for https://perlmonks.org/?node_id=11113260 # 02-20-2020 package Class1 { use warnings; use strict; sub new { my ($class, $config) = @_; print "Class1 called.\n"; my $self = { # set an error handler to be used by error(). # NOTE: anonymous subs are not member subs, so you +can't access $self... on_error => sub { print "on_error() : exiting with msg '$_[ +0]'\n"; exit(1); }, name => "<no-name-set>", }; bless $self => $class; # my idiomatic way to remember bless or +der, i.e. bless $self, $class $self->_init_from_config($config); return $self; } sub _init_from_config { my $self = shift; my $config = shift; # a hashref of config params return unless defined $config; for (keys %$config){ $self->{$_} = $config->{$_}, print "config: set '".$_."'.\ +n" if exists $self->{$_} } } sub myname { return $_[0]->{name} } sub error { my $self = shift; print "error() : called with $self, my name is ".$self->myname +()."\n"; $self->{on_error}->(@_); } } package Class2 { use warnings; use strict; use parent -norequire, 'Class1'; sub new { my ($class,$config) = @_; print "Class2 called.\n"; my $self = $class->SUPER::new($config); $self->{name} = "my name is Class2"; return $self; } } package Class3 { use warnings; use strict; use parent -norequire, 'Class2'; sub new { my ($class,$config) = @_; print "Class3 called.\n"; my $self = $class->SUPER::new($config); $self->{on_error} = sub { print "on_error() : warning with msg '$_[0]'\n"; }; return $self; } } package main; use warnings; use strict; use Data::Dumper; my $c2 = Class2->new(); my $c3 = Class3->new({ name => 'my name is Jack' }); print "here is c2:\n".Dumper($c2); $c3->error("oops for c3"); $c2->error("oops for c2"); print "you will not see this (because c2's error handler inherits c1's + which will exit()).\n";

bw, bliako


In reply to Re: Inheritable configuration options.... but with default values? by bliako
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.