Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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. gui.pl starts first. gui.pl is calling func() of A.pm

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

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

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

## gui.pl ##
use strict; use warnings; A::func();
## A.pm ##
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 receiver.pl forks sender.pl } 1;
## GuiModule.pm ##
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;
## receiver.pl ##
my $s = Server->new(); $s->initServer();
## sender.pl is same as receiver.pl but calls initClient()of Cleint.pm, so am skipping it
## Server.pm
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 Clinet.pm as well as investigating Server.pm 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 Server.pm 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 ?

Comment on original object getting changed as new process starts
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-09-21 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls