Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

original object getting changed as new process starts

by simonz (Sexton)
on Oct 04, 2013 at 18:18 UTC ( #1056910=perlquestion: print w/replies, xml ) Need Help??
simonz has asked for the wisdom of the Perl Monks concerning the following question:

I am am creating a global object and exporting it to a module which is initialized by another process. But in that module, the passed object is getting changed to a new object and am no longer able to access other attributes of the class it belongs to.
I am trying to explain the problem with an example step by step to make it clear :

1. starts first. is calling func() of

2. has a global variable $guimodule which is getting instantiated and exported.

3. func() of instantiate the class and builds the gui. It then forks two processes , a server and a client, through two scripts and respectively.

4. instantiates and uses the exported $guimodule to invoke some subroutine. But $guimodule is no longer the same $guimodule of hence the other attributes of can not be accessed.

## ##
use strict; use warnings; A::func();
## ##
use Exporter; @EXPORT = qw ($guimodule); my $mw = MainWindow->new(); sub func { $guimodule = GuiModule->new(parentWnd => $mw); $guiModule->initGui(); #constructs some columns of hlist $guimodule->populateGui(); #populate the tree forks forks } 1;
## ##
sub new { .... $self = {parentWnd=> $args{parentWnd}; } sub initGui { # creates other attributes as $self->{tree} = ... } sub populateGui { ...} sub changeGui { ## changeGui is getting invoked from Server::initServer() ## but $self->{tree} coming as undefined but before forking ## these t +wo process Dumper($self) is having the expected ##hash structure } 1;
## ##
my $s = Server->new(); $s->initServer();
## is same as but calls initClient()of, so am skipping it
use A; sub new { $class = shift; my $self = { .... }; bless $self; return $self ; } sub initServer { # starts the server using IO::Socket::INET ## accepts the connection if ($socket readable) { $guimodule->changeGui(); } } 1;
## skipping as well as investigating alone would solve the problem, I hope ##

The problem is in $guimodule object is having all the attributes of GuiModule properly created. But after calling the server and client processes $guimodule is not having the same structure and some attributes becoming undefined. I printed $guimodule before forking server/client from the GuiModule::new() and after forking from and GuiModule::changeGui(), these two are different objects of GuiModule as checked from the memory address. GuiModule=HASH(093xb48) and another GuiModule=HASH(0x3c388).
Why is the object getting changed and how can I resolve this ?

Replies are listed 'Best First'.
Re: original object getting changed as new process starts
by Anonymous Monk on Oct 04, 2013 at 18:38 UTC

    Why is the object getting changed and how can I resolve this ?

    You cannot resolve this, it can never work -- global variables are the devil -- see examples in Re: tk mainwindow not appearing, do it this way, or don't do it at all

    Never mind me, I'm tired

        Why erase your post when you can <strike> it out. Of course, that leaves the question of why post at all when you realize that your idea is wrong, but sometimes a wrong idea is still worth thinking about...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056910]
Approved by Old_Gray_Bear
Jar. Jar!...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2017-11-23 06:28 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (328 votes). Check out past polls.