Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

globalization sorrows

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

vaevictus has asked for the wisdom of the Perl Monks concerning the following question:

main file:
use strict; use Curses; use Mold; use vars qw ($mold); open (FILE, ">log.log") or die $!; open STDERR, ">&FILE" or die "Couldn't dup STDERR: $!"; $mold=Mold->spawn; $mold->config; $mold->play; $mold->fungicide; close FILE or die $!;
and Mold.pm loads a package eventually with:
package Standard; use strict; sub spawn { print $main::mold; }
which says uninitialized variable.

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

    Watching the CB I feel that what you are after is a Singleton. You can derive from Class::Singleton or just as easily roll your own.

    package Mold; our $Singleton; sub new { return $Singleton if defined $Singleton; my $class=shift; $Singleton=bless{},$class; $Singleton->init(@_); return $Singleton; } ... 1;

    Now all you have to do to get the same Mold object every time anywhere is to say Mold->new().

    This is not an uncommon pattern, and is conceptually identical to a global var, except that it gives you an interface isolation layer that makes your code much easier to maintain.


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: globalization sorrows
by vaevictus (Pilgrim) on Jul 06, 2003 at 18:42 UTC
    Mold->spawn:
    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 Standard.pm to inherit the spawn command.

    And my goal is to stop passing along $self.

      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.


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: globalization sorrows
by demerphq (Chancellor) on Jul 06, 2003 at 18:35 UTC

    The problem here is that when the line

    $mold=Mold->spawn;

    is executed $main::mold has never been assigned a value. If you executed that line twice you would find on the second time it would print the value 1, which would be the return value of the print statement from the first time it was executed. If you set $mold to be some value BEFORE you called spawn then it would print that value.

    I thought that Standard::spawn() was really Mold::spawn() here. My apologies. Theres not much we can do to help without knowing what Mold::spawn() looks like. Suffice it to say that this is a good example of checking to see if constructors actually suceed.

    $mold=Mold->spawn() or die "Mold wont spawn!";

    Would tell you if Mold::spawn() actually returned anything.


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: globalization sorrows
by vaevictus (Pilgrim) on Jul 06, 2003 at 18:32 UTC
    To be more specific on "loads a package eventually" ... Standard is inherited by other packages... which $mold->config () will cause to call the inherited Standard->spawn. But I don't seem to be able to get $main::mold to "be defined".
Re: globalization sorrows
by bobn (Chaplain) on Jul 06, 2003 at 20:04 UTC

    You've all lost me.

    Since Mold.pm doesn't define Mold->spawn, but instead defines Standard->spawn, why was this ever expected to work?



    --Bob Niederman, http://bob-n.com
Re: globalization sorrows
by vaevictus (Pilgrim) on Jul 06, 2003 at 18:48 UTC
    Full source code available via CVS at Sourceforge of this scary project. :D </shameless plug>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-01-22 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (63 votes). Check out past polls.

    Notices?