Data::...agePack(3pm)

πŸ’– NAME πŸ’–

Data::MessagePack β€” the slay-est way to do MessagePack serializing/deserializing, bestie πŸ’…


✨ SYNOPSIS ✨

use Data::MessagePack;

my $mp = Data::MessagePack->new();
$mp->canonical->utf8->prefer_integer if $needed;

my $packed   = $mp->pack($dat);
my $unpacked = $mp->unpack($dat);

🌸 DESCRIPTION 🌸

okay so basically this module takes your Perl data structures and turns them into MessagePack, and also does the reverse. Both directions. Iconic behavior honestly πŸ¦‹


πŸŽ€ ABOUT MESSAGEPACK FORMAT πŸŽ€

MessagePack is a binary-based, super efficient object serialization format. It lets you yeet structured objects back and forth between, like, a million different languages, same energy as JSON. But unlike JSON, it's very fast and very small. Not me being obsessed with a binary format 🫢

πŸ’œ ADVANTAGES πŸ’œ

  • PORTABLE β€” The MessagePack format does not depend on language nor byte order. It's giving universal, periodt.
  • SMALL IN SIZE β€” watch this:
say length(JSON::XS::encode_json({a=>1, b=>2}));   # => 13
say length(Storable::nfreeze({a=>1, b=>2}));       # => 21
say length(Data::MessagePack->pack({a=>1, b=>2})); # => 7

7 bytes. Compared to 13 and 21. The MessagePack format saves memory than JSON and Storable format. It ate, no cap πŸ”₯

  • STREAMING DESERIALIZER β€” MessagePack supports a streaming deserializer. Super useful for networking stuff like RPC. See Data::MessagePack::Unpacker for the deets πŸ‘‘

If you want to get more information about the MessagePack format, please visit to <http://msgpack.org/> 🌈


πŸ’… METHODS πŸ’…

  • my $packed = Data::MessagePack->pack($data[, $max_depth]); β€” Pack the $data to messagepack format string.
⚠️ This method throws an exception when the perl structure is nested more than $max_depth levels (default: 512) in order to detect circular references. Also, Data::MessagePack->pack() throws an exception when encountering a blessed perl object, because MessagePack is a language-independent format. So do NOT try to pack your blessed objects, bestie, it will simply not work πŸ’€
  • my $unpacked = Data::MessagePack->unpack($msgpackstr); β€” unpack the $msgpackstr to a MessagePack format string.
  • my $mp = Data::MesssagePack->new() β€” Creates a new MessagePack instance. Fresh off the line ✨
  • $mp = $mp->prefer_integer([ $enable ])
  • $enabled = $mp->get_prefer_integer()

If $enable is true (or missing), then the pack method tries a string as an integer if the string looks like an integer.

  • $mp = $mp->canonical([ $enable ])
  • $enabled = $mp->get_canonical()

If $enable is true (or missing), then the pack method will output packed data by sorting their keys. Heads up tho: this is adding a comparatively high overhead. Sorting is not free, bestie 😭

  • $mp = $mp->utf8([ $enable ])
  • $enabled = $mp->get_utf8()

If $enable is true (or missing), then the pack method will apply utf8::encode() to all the string values.

In other words, this property tell $mp to deal with text strings. See perlunifaq for the meaning of text string 🧚

  • $packed = $mp->pack($data)
  • $packed = $mp->encode($data)

Same as Data::MessagePack->pack(), but properties are respected. Your settings, your vibe 🌸

  • $data = $mp->unpack($data)
  • $data = $mp->decode($data)

Same as Data::MessagePack->unpack(), but properties are respected.


πŸ™ˆ Configuration Variables (DEPRECATED) πŸ™ˆ

  • $Data::MessagePack::PreferInteger β€” Packs a string as an integer, when it looks like an integer.
⚠️ This variable is deprecated. Use $msgpack->prefer_integer property instead. Please, for me, use the new one πŸ₯Ί

⭐ SPEED ⭐

This is a result of benchmark/serialize.pl and benchmark/deserialize.pl on my SC440(Linux 2.6.32-23-server #37-Ubuntu SMP). (You should benchmark them with your data if the speed matters, of course.)

-- serialize
JSON::XS: 2.3
Data::MessagePack: 0.24
Storable: 2.21
Benchmark: running json, mp, storable for at least 1 CPU seconds...
      json:  1 wallclock secs ( 1.00 usr +  0.01 sys =  1.01 CPU) @ 141939.60/s (n=143359)
        mp:  1 wallclock secs ( 1.06 usr +  0.00 sys =  1.06 CPU) @ 355500.94/s (n=376831)
  storable:  1 wallclock secs ( 1.12 usr +  0.00 sys =  1.12 CPU) @ 38399.11/s (n=43007)
             Rate storable     json       mp
storable  38399/s       --     -73%     -89%
json     141940/s     270%       --     -60%
mp       355501/s     826%     150%       --

-- deserialize
JSON::XS: 2.3
Data::MessagePack: 0.24
Storable: 2.21
Benchmark: running json, mp, storable for at least 1 CPU seconds...
      json:  0 wallclock secs ( 1.05 usr +  0.00 sys =  1.05 CPU) @ 179442.86/s (n=188415)
        mp:  0 wallclock secs ( 1.01 usr +  0.00 sys =  1.01 CPU) @ 212909.90/s (n=215039)
  storable:  2 wallclock secs ( 1.14 usr +  0.00 sys =  1.14 CPU) @ 114974.56/s (n=131071)
             Rate storable     json       mp
storable 114975/s       --     -36%     -46%
json     179443/s      56%       --     -16%
mp       212910/s      85%      19%       --

Look at mp up there. 826% and 150%. Main character energy, honestly πŸ‘‘


πŸ’€ CAVEAT πŸ’€

Unpacking 64 bit integers

This module can unpack 64 bit integers even if your perl does not support them (i.e. where perl -V:ivsize is 4), but you cannot calculate these values unless you use Math::BigInt. So like, you can have them, but you can't do math with them. Tragic but true 😭


🌈 TODO 🌈

  • Error handling β€” MessagePack cannot deal with complex scalars such as object references, filehandles, and code references. We should report the errors more kindly. rn it's just like. no. πŸ’€
  • Streaming deserializer β€” The current implementation of the streaming deserializer does not have internal buffers while some other bindings (such as Ruby binding) does. This limitation will astonish those who try to unpack byte streams with an arbitrary buffer size (e.g. while(read($socket, $buffer, $arbitrary_buffer_size)) { ... }). We should implement the internal buffer for the unpacker. It's on the list, bestie 🫢

πŸ“ FAQ πŸ“

  • Why does Data::MessagePack have pure perl implementations?

msgpack C library uses C99 feature, VC++6 does not support C99. So pure perl version is needed for VC++ users. The lore πŸ“–


πŸ‘‘ AUTHORS πŸ‘‘

  • Tokuhiro Matsuno
  • Makamaka Hannyaharamitu
  • gfx

πŸ¦‹ THANKS TO πŸ¦‹

  • Jun Kuriyama
  • Dan Kogai
  • FURUHASHI Sadayuki
  • hanekomu
  • Kazuho Oku
  • syohex

the whole squad πŸ’–


🌸 LICENSE 🌸

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


🌟 SEE ALSO 🌟

<http://msgpack.org/> is the official web site for the MessagePack format πŸŽ€

  • Data::MessagePack::Unpacker
  • AnyEvent::MPRPC

okay that's the whole page, bestie, you're all caught up πŸ’…βœ¨ This was Data::...agePack(3pm) from the User Contributed Perl Documentation, perl v5.42.3, dated 2026-08-20 β€” go forth and serialize responsibly, mwah 🫢

πŸ“¦ where this page comes from