Is there an equivalent to Perl's """
in Erlang?
I'd like to be able to define a pretty long string full of double-quotes without escaping every single one. Is that possible?
Feel free to let me know if I'm Doing It Wrong.
Thanks!
Is there an equivalent to Perl's """
in Erlang?
I'd like to be able to define a pretty long string full of double-quotes without escaping every single one. Is that possible?
Feel free to let me know if I'm Doing It Wrong.
Thanks!
I believe your best choice is to put your multi-line doublequote-full string into a separate file, and then read it with the new file:read_line, concatenating the lines at boot-up of your app.
Or if you want to have an über-mess, you can combine this with parse-transforms. You can place your string(s) into the source code, commented out and when the parse transform is invoked, you open up the source file, read out the text from the comments, concatenate and replace. Example:
...
Len = erlang:length("MY_FAKE_STRING_13"),
%% This is my "double-qouted"
%% "multi-line" string;
%% you know what I mean ;)
...
In your parse transform you look for strings starting with MY_FAKE_STRING. When you find one, you open up your module's source code, and read line's until you reach the very same string. Then read your source line-by-line until comments are coming, and concatenate them. Reaching the first empty (or non-comment) line, you have your string, which you can return instead of the fake string.
How about this:
1> atom_to_list('He said "hello" and then she answered "hi".').
"He said \"hello\" and then she answered \"hi\"."
You can define a macro to abbreviate atom_to_list for improved readability.
First I'd like to say that having such string quotation is not a bad idea for future versions of Erlang. But I'm not holding my breathe for it to arrive. Python makes good use of them as "unmunged" multi-line doc-strings.
If I was sufficiently annoyed by needing to escape quotes and backslashes I would look into working out a transforming macro in my editor to do it for me on the current text selection. Or maybe just implementing it with a regexp in sed to cut'n'paste. :)
$ echo 'Testing a "robust" way to \quote\ things.' | sed -e 's/[\\"]/\\&/g'
Testing a \"robust\" way to \\quote\\ things.
No.
There is basically no other way of doing it. The suggestions presented here work but seem more complex than the straight forward solution, and less clear.