Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Replacing namespaces

by smferris (Beadle)
on Dec 11, 2001 at 02:34 UTC ( [id://130801]=note: print w/replies, xml ) Need Help??


in reply to Replacing namespaces

Thanks for both of these responses! Both of them mention concerns that I've already thought about. None of them seem to be a problem.. Yet! 8) We'll see once I actually have the code soldered.

My initial code sample is actually incorrect. It would've been more appropriatly written:

#!/app/perl5.005/bin/perl package First; sub new { bless {}, 'First' }; sub test { my $self=shift; bless $self, 'Second'; $self->test; } 1; package Second; sub test { my $self=shift; print "Hello World!\n"; } 1; package main; my $t=new First(); print $t,"\n"; # Output: First=HASH(0x2864) $t->test or die $t->error; print $t,"\n"; # Output: Second=HASH(0xc2864)

I'm shielding the caller from having to try the different approaches manually. (Notice the fail over moved into First::test from main::. Also added a test on the return of the main:: call to test.) The fail-over "rules" will be determined by the failure type. an ORA-00001 (Unique constraint violation) may execute a new class that scrubs the data, then re-tries the First class again. (Ok.. I'd probably just make that a conditional and "truncate" the table ;) or throw the row to an exceptions table)

If the table to be loaded fails over to another approach, it will be logged and the configuration file can be updated to reflect which load process worked best. Bypassing First altogether when you know it's gonna fail.

Again, thanks for the responses.. The two links mentioned are really cool! (Both of them sound fairly close to my situation) Now it's off to code!

SMF 8)

Replies are listed 'Best First'.
Re: Re: Replacing namespaces
by dragonchild (Archbishop) on Dec 11, 2001 at 03:24 UTC
    NB:
    sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; }
    This allows for children classes to call you as a parent and have the inheritance work. You should NEVER hardcode anything. If you have to do something like bless {}, 'First';, at least do something like bless {}, __PACKAGE__;. That way, if you change the package name, you won't forget to change the name of the package you're blessing this thingy into.

    The reason to declare $self first is to allow for you to populate it. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re(2): Replacing namespaces
by dmmiller2k (Chaplain) on Dec 11, 2001 at 20:25 UTC

    Rather than hardcoding the fallback into the class itself, why not implement a dispatcher object, which, given the current class (available through caller()) and the kind of failure (?) does a lookup to determine the next class to try, to wit:

    my %FSM = ( First => { fail_a => 'Second', fail_b => 'Third', pass => undef }, # we're done Second =>{ fail_c => 'Third', pass => undef ) );

    Rather like a sparse-matrix.

    Then the code within any of the classes 'First', "Second' or 'Third' would merely consult the %FSM hash for what to do next. This brings up the problem of how (where?) to scope the %FSM hash. This should probably be a member within a common base-class:

    package Base; sub new { my $class = shift; my $self = { FSM => {First => { fail_a => 'Second', fail_b => 'Third', pass => undef }, # we're done Second =>{ fail_c => 'Third', pass => undef )} }; bless $self, $class; return $self; } package First; use base qw( Base ); sub new { # ... } package Second; use base qw( Base ); sub new { # ... } # ... and so on

    Any of the derived classes could access it as $self->{FSM}.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://130801]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found