Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Fellow monks,

I am writing some code where I have two processes created using fork() which communicate via sockets. I need to have only one of those processes execute functions, but the call to those functions may come from either process. So, I am trying to come up with a scheme to pass a function call, along with all arguments, from one process to the other via the socket. Here's what I have so far:

use warnings; use strict; use Socket; use IO::Handle; use Data::Dumper; socketpair(WORKER_SOCKET, GUI_SOCKET, AF_UNIX, SOCK_STREAM, PF_UNSPEC) + || die "socketpair: $!"; WORKER_SOCKET->autoflush(1); GUI_SOCKET->autoflush(1); if (my $pid = fork()) { close GUI_SOCKET; my $line = <WORKER_SOCKET>; if ($line =~ /^EXECUTE:(.*)$/) { # my $fx = "extracted function name"; # my @args = "extracted args" &{\&{$fx}}(@args); } } else { die "cannot fork: $!" unless defined $pid; close WORKER_SOCKET; # redirect the STDOUT of the WORKER script to GUI (and main Bacnet +Tool # communication) process. STDOUT->autoflush(1); my $a = 'baz'; my @b = qw(foo bar); my $c = \@b; print GUI_SOCKET "EXECUTE:doit($a, @b, $c)\n"; } sub doit { my @q = @_; print "\nDoing it " . Dumper(\@q); }
I have tried using the FreezeThaw module to serialize/deserialize the arguments on opposite sides of the socket. However, this does not work for arguments that are references, such as the 3rd argument in my example. Any ideas how else I can make such a remote function call

In reply to Executing functions from another process by gri6507

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (4)
    As of 2020-08-15 08:51 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (78 votes). Check out past polls.