Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day Amblikai,

"Is that something thats natively possible?"

Yes. The code below requires a minimum Perl version of 5.10.0 (for the mro pragma).

Update (correction): It actually needs a minimum version of 5.10.1 for the parent pragma; mro still needs a minimum of 5.10.0. I changed use 5.010; to use use 5.010_001; in the code.

#!/usr/bin/env perl use 5.010_001; use strict; use warnings; package My::Base; use mro; our %conf; sub new { my ($class) = @_; bless {} => $class; } sub configure { my ($class, $opts) = @_; { no strict 'refs'; %{$class . '::conf'} = (%{$class . '::conf'}, %$opts); } $_->configure($opts) for @{mro::get_isarev($class)}; return; } package My::Trunk; use parent -norequire, 'My::Base'; our %conf; package My::Branch_A; use parent -norequire, 'My::Trunk'; our %conf; package My::Branch_B; use parent -norequire, 'My::Trunk'; our %conf; package main; # Everything after here is for demo purposes only. use Data::Dump; say '*** Normal instantiation'; say 'My::Base object: ', My::Base::->new(); say 'My::Trunk object: ', My::Trunk::->new(); say 'My::Branch_A object: ', My::Branch_A::->new(); say 'My::Branch_B object: ', My::Branch_B::->new(); say "\n*** My::Base::->configure({A => 1});"; My::Base::->configure({A => 1}); _show_configs(); say "\n*** My::Trunk::->configure({B => 2, C => 3});"; My::Trunk::->configure({B => 2, C => 3}); _show_configs(); say "\n*** My::Branch_A::->configure({A => 'me', D => 4});"; My::Branch_A::->configure({A => 'me', D => 4}); _show_configs(); say "\n*** My::Branch_B::->configure({B => 'me', E => 5});"; My::Branch_B::->configure({B => 'me', E => 5}); _show_configs(); sub _show_configs { say 'My::Base::conf'; dd \%My::Base::conf; say 'My::Trunk::conf'; dd \%My::Trunk::conf; say 'My::Branch_A::conf'; dd \%My::Branch_A::conf; say 'My::Branch_B::conf'; dd \%My::Branch_B::conf; }

Note that all lowercase package names are generally reserved for pragmata (e.g. strict, warnings, etc.). I've replaced my::base with My::Base (and similarly for your other classes).

Output:

*** Normal instantiation My::Base object: My::Base=HASH(0x800003c98) My::Trunk object: My::Trunk=HASH(0x800003cc8) My::Branch_A object: My::Branch_A=HASH(0x800003c98) My::Branch_B object: My::Branch_B=HASH(0x800003cc8) *** My::Base::->configure({A => 1}); My::Base::conf { A => 1 } My::Trunk::conf { A => 1 } My::Branch_A::conf { A => 1 } My::Branch_B::conf { A => 1 } *** My::Trunk::->configure({B => 2, C => 3}); My::Base::conf { A => 1 } My::Trunk::conf { A => 1, B => 2, C => 3 } My::Branch_A::conf { A => 1, B => 2, C => 3 } My::Branch_B::conf { A => 1, B => 2, C => 3 } *** My::Branch_A::->configure({A => 'me', D => 4}); My::Base::conf { A => 1 } My::Trunk::conf { A => 1, B => 2, C => 3 } My::Branch_A::conf { A => "me", B => 2, C => 3, D => 4 } My::Branch_B::conf { A => 1, B => 2, C => 3 } *** My::Branch_B::->configure({B => 'me', E => 5}); My::Base::conf { A => 1 } My::Trunk::conf { A => 1, B => 2, C => 3 } My::Branch_A::conf { A => "me", B => 2, C => 3, D => 4 } My::Branch_B::conf { A => 1, B => "me", C => 3, E => 5 }
"I can't use Moose or any of the more advanced frameworks, i'm stuck with pretty basic perl unfortunately"

In that case, you may not have Data::Dump. To run the demo, change that to Data::Dumper and the dd ...; lines to print Dumper ...;.

See also: "perlootut - Object-Oriented Programming in Perl Tutorial" and "perlobj - Perl object reference".

— Ken


In reply to Re: Reconfiguration of classes in an "inheritable" manner by kcott
in thread Reconfiguration of classes in an "inheritable" manner by Amblikai

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-10-07 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (30 votes). Check out past polls.

    Notices?