tags:

views:

255

answers:

5

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!

A: 

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.

Zed
Please, don't use parse transforms for this. Apart from making the code totally unreadable they are also very non-trivial to make.It makes ten times more sense to use external text files in this case.
Adam Lindberg
I would agree, but the downside of the external file is that you need to modify your code in order to read the file only once. For example try to rewrite httpd_utils:reason_phrase() to use external files.Also code upgrade becomes non-trivial, because you need to invoke a re-read of the external file if you want to change the text, etc.
Zed
This solution is way too complex.
Christian
Yet I see no less complex solution given here for mimicking Perl's `"""` in Erlang.Anyway I don't want to shout illiteracy, but no one seems to understand that my primary answer was to move it to a separate file (except for maybe JLarky, or maybe not), and given the parse_transform way as the only solution I see for the `"""` question.
Zed
+2  A: 

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.

Cayle Spandon
This has the same downside as the external file solution. You either modify your code to do the conversion once (unless the constant atom-to-list conversion is acceptable). Also this way newline characters in your string become OS dependent, so you might encounter problems with Erlangs running on different OSs talking to each other.
Zed
A: 

Save your text in separate file

JLarky
+2  A: 

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.
Christian
+2  A: 

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.

rvirding
Thank you. I have to agree that all the solutions so far just make things more complicated.
i-g