Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
In Chapter 16: Class Hierarchies, subsection Inheritance (bottom p. 360) of Perl Best Practices, TheDamian recommends defining "a class's hierarchy declaratively at compile time, using the standard use base pragma. He argues that

"this approach discourages messing about with class hierarchies at run time, by reassigning @ISA. The temptation to modify @ISA at run time is usually a sign that your class might be better implemented as a factory, a façade, or with some other meta-object technique. (Emphasis added.)

Atypically, Damian doesn't provide an example of the code that would implement one of those other techniques. Not being experienced in design patterns, I'm somewhat at sea here. So I'm wondering if someone could provide a simple example of, say, the alternative to something like this:

BEGIN { use vars qw ( $VERSION @ISA ); $VERSION = '0.01'; require Solar::Data; require Lunar::Data; push @ISA, qw( Solar::Data Lunar::Data ); };

And later, at run time ...

if ($sun_rises_in_west) { require Astrological::Data; unshift @ISA, Astrological::Data; }

... called because, under certain unusual conditions, I want to make sure that the inheritance mechanism finds an Astrological::Data method (say, get_sunrise_time) before it finds Solar::Data::get_sunrise_time().

What would the alternative approach look like in code? Why, particularly in a simple case, would it be superior?

Thank you very much.

Jim Keenan


In reply to Modification of @ISA at run time by jkeenan1

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 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?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-29 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found