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 ?