If anyone ever tells you that regex syntax is the world's most unreadable embedded language, you can tell them it's not true.

The Common Lisp format utility is worse. It's analogous to sprintf, only much much more powerful. And unreadable.

Perl has \U and \L and \u for changing the case of parts of an interpolated string. format has features like that also.

format also has features for doing column justification. And automatic pluralization. And recursively calling a sub-format. And loops.

Here's an example from <cite>Common Lisp: The Language<cite>:

(defun format-error (string &rest args) ;Example (error nil "~?~%~V@T!~%~3@T\"~A\"~%" string args (+ *ctl-index* 3) *ctl-string*))
What's going on here? ~% means to start a new line. ~3@T means to tabulate over three spaces from the current position. (~3T would have meant to move to absolute position 3.) ~V@T is like ~n@T, except that inspead of moving over a specific number of spaces, the argument list of format is consulted for the number of spaces to move---in this case, the value is (+ *ctl-index* 3) spaces. ~A means to insert a string, in this case the value of *ctl-string*. ~? means to make a recursive call to format, using the format string in string, and the argument list in args, and insert the result.

Here's another delightful example from the same place:

(format stream ;; Are you ready for this one? "~:[{~;[~]~:{~S~:[->~S~;~*~]~:^ ~}~:[~; ~]~ ~{~S->~^ ~}~:[~; ~]~[~*~;->~S~;->~*~]~:[}~;]~]" ;; Is that clear? ;; I omitted the sprintf-style arguments here - MJD )
I won't explain this. But I will say that ~:[...~] is like a miniature switch statement, and ~:{...~} is like a miniature for loop, and ~:^ is like last.

For a complete description of format and an explanation of the examples in this note, see here. I recommend it. It has to be seen to be believed.

Now, dare I suggest that someone should write a Perl module that formats strings the way Common Lisp format does?

--
Mark Dominus
Perl Paraphernalia


In reply to Worse Than Regexes by Dominus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.