Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Modification of @ISA at run time

by TheDamian (Priest)
on Sep 01, 2005 at 06:50 UTC ( #488309=note: print w/replies, xml ) Need Help??

in reply to Modification of @ISA at run time

First some definitions:
A class that generates instances of other classes, choosing which class to build on the basis of run-time information.
A class that acts as a front-end for one or more other classes, delegating the actual work to those classes as appropriate.

In your particular example, what you want is a factory: a class that generates objects according to where the sun rises. For example:

# Rational helper class... package Astro::Data::Scientific; use base Solar::Data; use base Lunar::Data; # <define methods here> # "Differently rational" helper class... package Astro::Data::Mythological; use base Astrological::Data; # Pre-empt normal methods use base Astro::Data::Scientific; # <overload any methods here that the # heavens dictate should behave differently> # Factory class... package Astro::Data; # Constructor returns instances of helper classes, # depending on run-time astro[nl]o[mg]ical conditions sub new { shift; # off class name if ($sun_rises_in_west) { return Astro::Data::Mythological->new(@_); } else { return Astro::Data::Scientific->new(@_); } } # <no methods of its own, since it's never actually instantiated>

Replies are listed 'Best First'.
Re^2: Modification of @ISA at run time
by jkeenan1 (Deacon) on Sep 01, 2005 at 12:30 UTC
    Thanks, Damian and all the others who took the time to reply.

    I think I was mostly thrown by the unfamiliar nomenclature. It turns out that I've already used the factory concept (in projects other than the current one) without knowing the fancy name. So I shouldn't have too much trouble implementing one now.

    Jim Keenan

Re^2: Modification of @ISA at run time
by jkeenan1 (Deacon) on Sep 02, 2005 at 04:02 UTC
    Here is a cleaned-up version of Damian's example, along with a test.

    I hope this example is (a) correct and (b) useful to the next person who wonders about this.

    Jim Keenan

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://488309]
[stevieb]: perldigious You should start by writing some unit tests. That'll ensure current functionality doesn't break with changes.
[choroba]: unit tests++
[choroba]: The only problem you can't solve by adding more tests is the problem of having too many tests.
[stevieb]: choroba Been there, done that :) Better too many than not enough though!
[stevieb]: I very often write a base set of unit tests before writing any code. It helps dictate the API, and ensures my earliest of ideas stay on track.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-07-21 20:00 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (335 votes). Check out past polls.