Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: globalization sorrows

by vaevictus (Pilgrim)
on Jul 06, 2003 at 18:42 UTC ( #271799=note: print w/replies, xml ) Need Help??

in reply to globalization sorrows

sub spawn { my $this=shift or die; my $class=ref($this) || $this; my $self = { }; bless $self,$class; $self->{parent}=$self || die; $self->{mold}=$self || die; $self->init; return($self); }
and Mold->init():
sub init { my $self=shift; $SIG{__DIE__} = sub {endwin; }; $SIG{__WARN__} = sub { endwin; warn @_; my $i = 0; while (my($pkg, $file, $line) = caller($i++)) { warn " package $pkg, file $file, line $line\n"; } }; $self->{prefs} = Prefs->spawn($self); $self->{prefs}->load_rc(); $self->{prefs}->load_terrains($self->{prefs}->{datadir} . "/terrain +s"); $self->{uic} = UI_Curses->spawn($self); $self->{com_int} = Command_Interpreter->spawn($self); $self->{grid} = Grid->spawn($self); $self->{com_proc} = Command_Processor->spawn($self); $self->{player} = Player->spawn($self); $self->{eq} = EventQueue->spawn($self); $self->{data_monsters} = Data::Monsters->spawn($self); $self->{msg} = Message->spawn($self); $self->{eq}->addclass('thingy'); }
now... see why i didn't want that thrown in? ;)

every one of those spawn commands use to inherit the spawn command.

And my goal is to stop passing along $self.

Replies are listed 'Best First'.
Re: Re: globalization sorrows
by demerphq (Chancellor) on Jul 06, 2003 at 19:02 UTC

    Look at the order of events here. BEFORE $main::mold gets assigned a value the following calls are made:

    Mold::spawn Mold::init Prefs::spawn UI_Curses::spawn Command_Interpreter::spawn Grid::spawn Command_Processor::spawn Player::spawn EventQueue::spawn Data::Monsters::spawn Message::spawn

    So if any of those subs expect $main::mold to already have a value they would be wrong.

    Im guessing that you should either alter Mold::spawn so that it says

    sub spawn { my $this=shift or die; my $class=ref($this) || $this; my $self = bless {},$class; $main::mold=$self; $self->init() return($self); }

    Or remove the call to init() from the constructor and change the code in main to be

    $mold=Mold->spawn; $mold->init();

    Also the self references that you put in there

    $self->{parent}=$self || die; $self->{mold}=$self || die;

    will cause a memory leak unless they are eventually changed.

    A better design pattern for things like that is to use a pattern where the self referential structure is not itself the root object. Then the DESTORY method in the root object can safely remove the self references and the whole object will destruct when it falls out of scope.


    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-01-24 15:13 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (64 votes). Check out past polls.