note
BrowserUk
<blockquote><i></i></blockquote>
<p>As [SuicideJunkie] suggest, you were probably trying to use line-oriented xfer functions (ie. [print] and [readline] ) on a [binmode]d socket.
<p>My recommendation would be to use [pack]/[unpack] & [send]/[recv] like this:<code>
$to->send( pack 'n/a*', $binData );
...
$from->recv( my $len, 2 );
$from->recv( my $binData, unpack 'n', $len );
</code>
<p>That's good for packets up to 64k in length. Switch to 'N' to handle up to 4GB.
<P>The nice thing about this is that the receiver always knows how much to ask for; and can verify that he got it (<c>length $binData</c>) which avoids the need for delimiters and works just as well with non-blocking sockets if you need to go that way.
<h3>Important update: If using this method to transmit data between machines, see also the thread at [id://987794]</h3>
<p>I also found that when it comes to transmitting arrays and hashes, using [pack]/[unpack] is usually more compact (and therefore faster) than using [mod://Storable], because (for example) an integer always required 4 or 8 bytes binary, but for many values it is shorter in ascii:<code>
use Storable qw[ freeze ];;
@a = 1..100;;
$packed = pack 'n/(n/a*)', @a;;
print length $packed;;
394
$ice = freeze \@a;;
print length $ice;;
412
@b = unpack 'n/(n/a*)', $packed;;
print "@b";;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ...
%h = 'aaaa'..'aaaz';;
$packed = pack 'n/(n/a*)', %h;;
print length $packed;;
158
$ice = freeze \%h;;
print length $ice;;
202
%h2 = unpack 'n/(n/a*)', $packed;;
pp \%h2;;
{
aaaa => "aaab",
aaac => "aaad",
aaae => "aaaf",
aaag => "aaah",
aaai => "aaaj",
aaak => "aaal",
aaam => "aaan",
aaao => "aaap",
aaaq => "aaar",
aaas => "aaat",
aaau => "aaav",
aaaw => "aaax",
aaay => "aaaz",
}
</code>
<p>It doesn't always work out smaller, but it is usually faster and platform independent.
<p>Of course, storable wins if your data structures can contain references to others.
<div class="pmsig"><div class="pmsig-171588">
<hr />
<font size=1 >
<div>With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'</div>
<div>Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.</div>
<div>"Science is about questioning the status quo. Questioning authority". </div>
<div>In the absence of evidence, opinion is indistinguishable from prejudice.
<p align=right>[http://www.theregister.co.uk/2011/11/29/sas_versus_world_programming/|The start of some sanity?]</p></div>
</font>
</div></div>
987602
987642