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

Hello All. Is there any way to send a data structure(array or hash) via a socket? I know how to send something line by line but how do you send a "whole structure"? Or is "receiver have to read each line as it is sent and and interpret it" the only way. I dont want to send any files. Thanks Sawan

Replies are listed 'Best First'.
Re: Sending data structures via sockets
by rob_au (Abbot) on Jan 18, 2003 at 08:32 UTC
    The best answer which I can offer is ... It depends.

    If you are referring to the transmission of Perl data structures such as arrays and hashes, you may find some worth in the Storable module which allows such data structures to be serialised into a 'flat' form for such transmission. Alternatives to this module include Data::Dumper and FreezeThaw.

    If you are referring to a plain block of information, it may be worth looking at encapsulating such data into a discrete format which can be recognised at the other end of the socket and unpacked accordingly. A simple example of such a format would be uuencoding often used in mail transmission where the encoded data is transmitted between header BEGIN and trailer END lines of text.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000011010"))'

Re: Sending data structures via sockets
by poj (Abbot) on Jan 18, 2003 at 14:01 UTC
    You could use XML, there are many ways depending on the complexity of your data. A simple XML solution would be something like this ;
    #c:/perl/bin/perl -w use strict; use XML::Dumper; my $dumpobj = new XML::Dumper; my $hashref_send = [ { fname => 'Fred', lname => 'Flintstone', residence => 'Bedrock' }, { fname => 'Barney', lname => 'Rubble', residence => 'Bedrock' } ]; # convert to XML string for sending my $xml_msg = $dumpobj->pl2xml( $hashref_send ); # or Dump to a file for debugging my $file = "dump.xml"; $dumpobj->pl2xml( $hashref_send, $file ); # Convert received XML back to hash my $hashref_rec = $dumpobj->xml2pl( $xml_msg ); # Check the result use Data::Dumper; print Dumper($hashref_rec); print $$hashref_rec[0]{'fname'};
    If you are using ActiveState, you need to install 2 modules
    XML::Parser - use PPM
    XML::Dumper - don't use PPM because early version are broken, instead download the source from CPAN as Dumper.pm and save it in C:\Perl\site\lib\XML
    poj
•Re: Sending data structures via sockets
by merlyn (Sage) on Jan 18, 2003 at 14:34 UTC
Re: Sending data structures via sockets (SOAP)
by gjb (Vicar) on Jan 18, 2003 at 14:59 UTC

    Soap::Lite is very nice to use, it takes virtually no effort to program a client/server system (i.e. just some ten lines of code if both client and server are implemented in Perl.

    Apart from receiving data structures as result from remote methods, you can even operate on remote data structures from the client as if they were local, the only difference being a few lines of code to indicate that they're remote objects.

    Below you'll find a Node class (to build a tree with) and a SOAP client and server script to demonstrate this.

    Just my 2 cents, -gjb-