Updated 2012-06-28 12:19:17 by RLE

(started by TV)

To counter the notion 'quoting is bad' or 'quoting will make you go to hell' and other such nonsense, I thought it would be a good idea, also in the face of determining theoretical considerations and practical requests for quoting related issues from the positive side of things, to discuss why one wants quoting, and of course how far we have to be denied sugar-candy-heaven lookalikes and how far excuses can go for people to claim their heaven to be as obnoxious or obscure about their programming games as they can, in this case in syntactic or grammar sense related to quoting.

I think I like the idea behind this page; but was the above babel-fished from Dutch (or Klingon)? How about some punctuation heaven?

What is this 'heaven to be', and why would people be claiming it to be obnoxious or obscure?

Maybe remarks can be freely moved in between heaven or hell pages according to good public gouvernment ruling...

The Main Reasons for quoting are, or have to do with, without much question:

  • substitution
  • evaluation
  • grouping of segments of data or programs

The simplest forms:
 set list {a b c}
 set string "a b c"
 set listasstring "{a b c}"     ;# a string construction as a valid list
 set listwithstring {"a b c"}   ;# a decent statement with a list containing one element: a string

DGP That forumulation is misleading. It suggests that different quoting characters have the power to give types to Tcl values. They do not. Brace-quoting doesn't make a list, and DoubleQuote-quoting doesn't make a string. This is fundamental to understanding Tcl. More truthful is:
 set literalWord {a b c}
 set substitutedWord "a b c"

The double quoted examples just amplify the confusion. Compare these:
 set example1 "{a b c}" ;# your "listasstring"
 set example2 {{a b c}} ;# different quoting - exact same result
 set example3 {"a b c"} ;# different string, but same list

Can't repeat it too often, the quoting chars chosen control substitution. They have zero impact on the "type" of the result, because Tcl values do not hold any "type" data.

Less Obvious or Advanced Reasons for quoting probably include:

  • syntax analysis
  • constructing of program pieces, like in -command Tk options
  • mathematical or other symbolic manipulations
  • advanced list manipulation

Why 'heaven'? Well, because of Tcl being based on interpreted strings and lists, the programmer is fairly free to do what he wants while dealing with the main things that make up programs: characters and their interpretation.

Of course there are, for some liberal, for some conservative, rules to behold this freedom, such as when one can solve programming problems in a neat way, don't go to (quoting or other) hell and don't send the readers or maintainers of your coding efforts there unless you think that is right.

When messing about with C strings, or as in another well known interpreted language, Basic strings and commands, one easily missed the few extra quotes or backslashes a tcl-er can use in his evaled statements, to make them reach over more levels of interpretation, such as also known in the Lisp area.

Let's list some examples.

Examples

A simple web page generating statement using puts and clock
    puts "<html>\n<h2>My Web Page</h2>\n\[clock format \[clock seconds\]\]:\n[clock format [clock seconds]]\n</html>"
 <html>
 <h2>My Web Page</h2>
 [clock format [clock seconds]]:
 Wed Oct 27 17:11:13 West-Europa (zomertijd) 2004
 </html>

How about comparing quoting techniques in other scripting languages and compare and contrast the alternatives?

CLN OK, I'll bite. One of the JO's greatest choices in designing Tcl was using asymetrical evaluation tokens (one to start, a different one to end). That is, whereas most UNIX shells use backquote to mean "evaluate and substitute the result here", Tcl uses [ and ]. Because the tokens are asymetrical, they can be nested. In csh, et al., you can't very well do:
   x=`grep `cat somefile` *.h`

because the `s don't go where you want them. But in tclsh, you can certainly do:
   set x [myproc [yourproc $anArg]]

Heaven! ;-)

glennj To compare Tcl and Perl quoting:

Tcl:
 set x "hello \"$world\""

Perl:
 $x = "hello \"$world\"";
 $x = qq{hello "$world"};

Tcl:
 set x {an [uninterpolated $string] 'with single quotes'}

Perl:
 $x = 'an [uninterpolated $string] \'with single quotes\'';
 $x = q{an [uninterpolated $string] 'with single quotes'};

In its spirit of TIMTOWDI, Perl also has here documents that can be interpolated or not.

See also Cloverfield, section Word modifiers, for an heredoc-like syntactic improvement. In short:
set s {data}MyTag
some arbitrary data {\$[#
MyTag

You can enclose verbatim data between arbitrary tags. One could for example choose to use the MD5 hash of the data as a tag. Combined with Critcl and tcc this can greatly simplify the integration of foreign language. We could also include raw XML data to feed tdom or TclXML with.

Jim provides a similar quoting syntax:
set v {<<EOT}{
blah blah...
any old stuff: $$$ {{{ [[[ \\\ ]]] etc.
EOT}

Meta-remark: the Wiki provides such a feature with the 6-equal marker.