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>:
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.(defun format-error (string &rest args) ;Example (error nil "~?~%~V@T!~%~3@T\"~A\"~%" string args (+ *ctl-index* 3) *ctl-string*))
Here's another delightful example from the same place:
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.(format stream ;; Are you ready for this one? "~:[{~;[~]~:{~S~:[->~S~;~*~]~:^ ~}~:[~; ~]~ ~{~S->~^ ~}~:[~; ~]~[~*~;->~S~;->~*~]~:[}~;]~]" ;; Is that clear? ;; I omitted the sprintf-style arguments here - MJD )
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid @#$^*!!!) Re: Worse Than Regexes
by Ovid (Cardinal) on Dec 15, 2001 at 01:36 UTC | |
|
Re: Worse Than Regexes
by boo_radley (Parson) on Dec 15, 2001 at 03:16 UTC | |
by Dominus (Parson) on Dec 15, 2001 at 06:22 UTC | |
|
Re: Worse Than Regexes
by mr_mischief (Monsignor) on Dec 18, 2001 at 09:55 UTC |