Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: How can I seralize a file for use with XML?

by repellent (Priest)
on Feb 01, 2012 at 06:18 UTC ( #951125=note: print w/replies, xml ) Need Help??

in reply to How can I seralize a file for use with XML?

The prevalent XML 1.0 spec forbids the use of most "control" characters (in the range #x00 - #x19), and that makes it a terrible format for shipping binary data. Even if your API requirement starts off as being printable text-only, you'd soon slam hard into this limitation once you demand more out of your XML use.

The workaround is to encode your binary data (e.g. Base64) into an XML-compatible string that you can embed in the XML. This introduces an extra decoding step when you want to get back at your binary data.

    ... add the seralized files to a hash, convert the hash to XML, print the XML to a socket, then, on the other end of the socket, convert the XML back to a perl hash and deseralize the files.

Consider that what you want is to get that Perl hash safely across. Why not just ship binary data across using Storable?
use Data::Dumper; use Storable qw(nfreeze thaw); my $request = { TYPE => 'UPLOAD', DATA => { file1 => 'content1', file2 + => 'content2' } }; my $serialized = nfreeze $request; # send this binary string my $deserialized = thaw $serialized; # round trip! print Dumper $deserialized;

Or use a format like JSON? See JSON::XS.

If all you care about is to ship the files across, then consider using Archive::Tar with compression as a way of packaging the files. The tar object can read/write from filehandles and would work with sockets.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951125]
[Eily]: Discipulus that's a chess joke BTW right? Because bishops don't walk straight :D
[choroba]: stop making chessy jokes about bishops!
[choroba]: I mean cheesy
[LanX]: lol
[ambrus]: no, the chess bishop itself is a joke on real bi-shops
[ambrus]: or at least on the stereotype of bi-shops
[choroba]: we call chess bishops "archers"
[Eily]: choroba sorry, I had to get that off my chess
[ambrus]: chess certainly hasn't started those stereotypes, like how kings aren't that powerful but has convinced their whole country to work for them, etc. it's just poured them to a nice clean form that would easily get propagated.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2017-09-26 12:17 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (294 votes). Check out past polls.