gokuraku has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I'm working on a small test client/server in Perl and have come up on a roadblock. I would like to send some data to the Server and can get this to work with manual input, I have it as bidirectional so that input is sent on one process and echoed back on another; I got this from the Perl Cookbook - Recipie 17.10. This works fine, and I can run mulitple clients against my server. Now I'd like to use an Array with some data to output into the client and send it to the server, basically this will allow me to have some communication across the client and server. I can't seem to figure out the right process by which I can get the array to write out to the client's file handle. Doing it manually I run:
while (defined($line= <STDIN>)) { print $rem_socket $line; }
But how can I get this to work in an automated fashion? I thought I could just do:
foreach $send (@data) { print $rem_socket $send; }
So each line in the array, its a set of strings, is sent to the server. Hints and help are appreciated. Thanks!

Replies are listed 'Best First'.
Re: Redirect array to a socket handle
by ikegami (Patriarch) on Nov 05, 2007 at 19:49 UTC

    You need to somehow convert your array into a stream a bytes which can later be converted back into a copy of the original array. That process is called serialization (not redirection). Storable provides a means of doing this.

    Sender:

    use Storable qw( nstore_fd ); nstore_fd(\@data, $rem_socket);

    Receiver:

    use Storable qw( fd_retrieve ); my $data = fd_retrieve($rem_socket);
Re: Redirect array to a socket handle
by FunkyMonk (Bishop) on Nov 05, 2007 at 19:48 UTC
    Do the elements of @data end in a newline? Perhaps
    print $rem_socket "$send\n";

    is what you need.

      Hadn't thought about the newline but now that I see it I am not sure how I missed it...thanks this did the trick for me.
Re: Redirect array to a socket handle
by codeacrobat (Chaplain) on Nov 06, 2007 at 07:09 UTC