In a nutshell yes. I want to be able to write a term to disk as text. Then read it again later, the file needs to be human-readable. I can explain why if you are interested.
A bit of info will be helpful. The way to handle (most) terms in a human readable way is to use inspect to dump the data to disk, and then Code.eval_string to turn it back into a value. However this has a variety of trade offs and won’t handle arbitrary terms because things like pid values for example don’t have an output from inspect that will turn back into a pid when evaled. Whether that matters or not depends on your use case.
If you think quoted expression is human-readable. Try to use:
quote
Code.eval_quoted
Or, just use :erlang.term_to_binary and :erlang.binary_to_term. When you want to inspect it, use something like following code instead of reading it directly.
Basically we have a deployment that is in a very secure enclave. We don’t have remote shell access and the normal ways you would debug an Elixir application.
What we do have is the ability to view logs. What I had thought is having the ability to write to a log terms that need analyzing and re-load them for later analysis at work (well my desk in the living room). It would allow us to replay the terms locally.
However, if I’m wiling to lose the ability to make whatever is logged human-readable, I think I can do this:
b64term = term |> :erlang.term_to_binary() |> Base.encode64()
File.write("/tmp/foo.term", b64term) # this is really a log file
Then
term = File.read!("/tmp/foo.term") |> Base.decode64!() |> :erlang.binary_to_term()
Gotcha! Yeah so for this purpose I would definitely use term_to_binary/1 because this will be a far more reliable way of getting the exact values loaded into a shell later. You can always make it human readable on your local machine by inspecting the value you loaded via binary_to_term/1
I’d use ~tp.~n so there is unicode characters. and a newline between terms. also you don’t need to flatten the iostring prior to write.
Most Erlang config files are written like this and read back by consult. It is very nice and simple. Or you can use other serialization format like json, or toml but they don’t support all erlang types.