Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Worse Than Regexes

by Dominus (Parson)
on Dec 15, 2001 at 00:57 UTC ( [id://132074]=obfuscated: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
(Ovid @#$^*!!!) Re: Worse Than Regexes
by Ovid (Cardinal) on Dec 15, 2001 at 01:36 UTC

    I just love seeing stuff like this. It's a great way of bringing home a point that seems to confound many programmers: Joe Programmer often claims that because he can't read language foo, it must be bad. If Joe can't read it, then Joe can't read it and that's about all one can infer from that statement. I've heard fascinating things about Lisp and Haskell and I've played around with Prolog (great language!). None of these have, to me, an intuitive syntax. If people avoid languages because they look scary, no one would ever have started programming; even BASIC can look scary if you've never programmed :)

    Cheers,
    Ovid

    Side note: I now think BASIC looks scary because I have programmed.

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Worse Than Regexes
by boo_radley (Parson) on Dec 15, 2001 at 03:16 UTC
    Everytime someone complains about perl's syntax, I smugly reply "Complex powerful concepts occasionally require complex powerful syntax", and then chuckle knowingly.
    Observations:
    1. Based on the thought above, I think Common Lisp is to perl as Superman is to the common man.
    2. I also like that the Common Lisp steering commitee seems to be the clearly titled "X3J13".
    3. They seem to have some knowledge of the brown-bagger beauty of this syntax : Here is a blow-by-blow description of the parts of this format string
    4. For more fun, search out "obfuscated lisp" on google.

    thanks, Dominus!

    2001-12-14 Edit by Corion : Added missing </OL> tag

      Says boo_radley:
      I also like that the Common Lisp steering commitee seems to be the clearly titled "X3J13".
      That wasn't the Common Lisp folks' doing; that's how ANSI names their committees. The X3 groups are all involved with various aspects of information technology, and the X3J groups are all standardization committees for various programming languages. The committee that produced the 1989 C standard was X3J11.

      --
      Mark Dominus
      Perl Paraphernalia

Re: Worse Than Regexes
by mr_mischief (Monsignor) on Dec 18, 2001 at 09:55 UTC
    This reminds me of some nodes around here on BrainF**k, namely the nodes BrainF**k Interpreter and BrainF**k question.

    If anyone thinks that Perl is hard to read, they've never used BrainF**k, TECO, INTERCAL (which would be a good thing not to use), or any of a host of other very capable and sometimes powerful (apl comes to mind) languages and tools.

    Sometimes it's good to do things for beginners, but when that gets in the way of advanced use, you either get users leaving the tool or, unfortunately, piling grotesque extensions onto a design poorly suited to them. Hence you have things like Turbo Pascal, Realizer, and Ada. ;-) I'm glad Perl has been extended through modules for exactly this reason, and that the core language has somewhat of a learning curve. I don't want an OO, parallel, highly optimizing Logo installation with visual tools and a full debugging suite.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://132074]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found