Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

To put the earlier answers in context, the only thing you can send over a socket is bytes. Sockets are file handles, and (system) file handles can only handle streams of bytes. That means you need convert your data into a stream of bytes that contains enough information for the received to reconstruct the data. The process of converting the data to a stream of bytes is called serialization. The process of recreating the data from the stream of bytes is called deserialization.

Storable is a module used to serialize arbitrary Perl data structures. I think it produces rather compact strings. pack/unpack basically reinvent the Storable wheel, but can be used to produce custom solution such as something even more compact or something that matches an existing serialization method.

Storable is not so good if you need to interface with something other than Perl. That's why JSON was mentioned. It's a well defined format that has been ported to many languages and will handle fairly complex structures. Instead of being compact, it's human readable. XML and YAML also share all those characteristics.

One advantage of Storable is that it handles embedding the serialized data in the middle of a data stream natively. In most other cases, you'll need some kind of framing to signal the end of the data structure if you intend to keep using the socket. This is often done by preceding the serialized data with the size in bytes of the serialized data.

# Sender: my $serialized = serialize($data); print $sock pack( 'N', length($serialized) ), $serialized;
# Receiver: my $data = do { my $length = do { my $buf; sysread( $sock, $buf, 4-length($buf), length($buf) ) while length($buf) < 4; unpack( 'N', $buf ) }; my $buf; sysread( $sock, $buf, $length-length($buf), length($buf) ) while length($buf) < $length; deserialize($buf) };

In reply to Re: Send an Array of Hash in Socket by ikegami
in thread Send an Array of Hash in Socket by muthuma

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found