In Perl, pack
and unpack
are two functions for transforming binary data according to a user-defined template.
Here is a tutorial on these functions: https://perldoc.perl.org/perlpacktut#The-Basic-Principle
What is the idiomatic Clojure way of doing the same things these two functions do?
It is uncommon for people to invent their own serialization format in Clojure. Instead the most common solutions will serialize existing datastructures to text/binary and back.
The built-in version into Clojure is the EDN format, which is a subset of clojure itself.
(pr-str {:foo "bar"})
gives you "{:foo \"bar\"}"
. clojure.edn/read-string
for that string gives you back the original map.
If you'd prefer a little more compact and slightly more performant variant then transit is a good choice. Same principle as EDN as it has full built-in support for all CLJ datastructures and can be extended for custom types.
If you'd rather really want to use your own custom binary format, you could fall back to using java.io.DataOutputStream and DataInputStream respectively. Of course there are also a lot of libraries that would simplify/abstract this further.