#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Help on object serialization and wriring to pipe


    Hi ,

    Please let me know how can I send the $guiobject object so as it remanis unchanged when I access it from server.pm . As you can see the object printed by two print messages from CommSystem and Server.pm are different.

    I need a mechanism to have the same object ,created in CommSystem.pm , in Server.pm. I got to know that it is possible by object serialization and writing to pipe, please help me how can i achieve that or else any other mechanism is also fine.

    Below is my code :

    ## start.pl

    Code:
    #!/usr/bin/perl  
    use strict; 
    use warnings;  
     
     
    use CommSystem;    
    CommSystem::init();
    ## CommSystem.pm
    Code:
    package CommSystem;   
    use strict; 
    use warnings;  
    use Tk; 
    use Exporter;  
    use GUI;   
     
    our @ISA = qw (Exporter);    
    our @EXPORT = qw ($guiobj);  
    our $guiobj;    
     
    my $mw = MainWindow->new();    
    $guiobj = GUI->new();    
    MainLoop; 
    sub init  {          
     
    print "Sent object $guiobj \n";          
     
    system ("perl proc.pl");  
     
    }    
     
    1;
    ## GUI.pm

    Code:
    package GUI;  
     
    use strict;  
    use warnings;    
     
    sub new  {      
     
    my $class = shift;      
     
    my $self = {} ;      
     
    bless $self;      
     
    return $self;  
     
    }    
     
    1;
    ## proc.pl

    Code:
    #!/usr/bin/perl  
     
    use strict;  
     
    use warnings;    
     
    use Server;  
     
    my $server = Server->new();  
     
    $server->initServer();

    ## Server.pm

    Code:
    package Server;    
     
    use strict;  
     
    use warnings;  
     
    use CommSystem;    
     
    sub new  {      
     
    my $class  = shift;          
     
    my $self = { object => $guiobj };      
     
    bless $self;        
     
    return $self;    
     
    }    
     
     
    sub initServer   
    {    
     
    my $self= shift;    
     
    print "passed object ",  
     
    $self->{object}, "\n";  
     
    }  
     
    1;
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,254
    Rep Power
    1810
    Code:
    sub init  {          
       print "Sent object $guiobj \n";          
       system ("perl proc.pl");  
    }
    You are never passing an object, you are just trying to export an existing global. But you are doing so from a new process, so that could never contain the same object.

    I'm not sure what sort of architecture you're going for, but you might want something like a Singleton.

    http://perldesignpatterns.com/?SingletonPattern

    That's where a class can only contain a single instance. You can call it from multiple places but still receive the one, unique object if it has already been instantiated. The Logger module uses that pattern, I believe.

    It still needs to be a single perl process, otherwise you will be limited to passing by value.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Help on object serialization and writing to a pipe


    Thanks for the reply Keath and suggesting the design pattern.

    Yes you are right, am not passing the object and exporting an existing gloabl.

    That is because I am finding it difficult to send the object to a process without the object being changed. Any help on that will be very useful.

    Thanks once again.
  6. #4
  7. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,254
    Rep Power
    1810
    I am finding it difficult to send the object to a process without the object being changed.
    And I'm finding it hard to understand that. Could you provide an example? It would be much easier to see where the problem is.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by keath
    And I'm finding it hard to understand that. Could you provide an example? It would be much easier to see where the problem is.
    Well , from CommSystem even if I try to pass $guiobj to proc.pl as system("perl proc.pl $guiobj"), it would go as a string and not as an object. So how can I receive $guiobj in Server.pm ?
  10. #6
  11. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,254
    Rep Power
    1810
    You really can't.

    I mean there is a sort of shared objects module on CPAN that allows objects to be used across threads, so I can't say it is completely impossible, but in general you can not share memory across processes in your computer. Two different perl processes are essentially two completely different programs. They could communicate by sending data, but not sharing memory.

    I don't use Tk, so I don't know if what you are trying to accomplish is related to that. If it is, I would guess there is something wrong with your architecture because I doubt many people would get it working if it was that complicated.

    You can simulate multiple threads using POE. There are examples using Tk here:

    http://poe.perl.org/?POE_Cookbook/Tk_Interfaces

    In that way, you really would be using a single process and could share any objects.

    ---

    And of course we can communicate even from one machine to another, but we do it through data messaging. We might be able to provide more help if we knew what the big picture was. What kind of program are you trying to write?

IMN logo majestic logo threadwatch logo seochat tools logo