Leaning toothpick syndrome

From Seo Wiki - Search Engine Optimization and Programming Languages

Jump to: navigation, search

In computer programming, leaning toothpick syndrome (LTS) is the situation in which a quoted expression becomes unreadable because it contains a large number of escape characters, usually backslashes ("\"), to avoid delimiter collision.

The official Perl documentation[1] introduced the term to wider usage; there, the phrase is used to describe regular expressions that match Unix-style paths in which the elements are separated by forward slashes.

LTS appears in many programming languages and in many situations, including in patterns that match Uniform Resource Identifiers (URIs) and in programs that output quoted text. Many quines fall into the latter category.


Pattern example

Consider the following Perl regular expression intended to match URIs which identify files under the pub directory of an FTP site:


Perl solves this problem by allowing many other characters to be delimiters for a regular expression. For example, the following three examples are equivalent to the expression given above:


Quoted text example

A Perl program to print an HTML link tag, where the URL and link text are stored in variables $url and $text respectively, might look like this. Notice the use of backslashes to escape the quoted double-quote characters:

 print "<a href=\"$url\">$text</a>";

Using single quotes to delimit the string is not feasible, as Perl does not expand variables inside single-quoted strings.

 print '<a href="$url">$text</a>'

Using the printf function is a viable solution in many languages (Perl, C, PHP):

 printf('<a href="%s">%s</a>', $url, $text);

The qq operator in Perl allows for any delimiter:

print qq{<a href="$url">$text</a>};
print qq|<a href="$url">$text</a>|;
print qq(<a href="$url">$text</a>);

Here documents are especially well suited for multi-line strings; however, here documents do not allow for proper indentation. This example shows the Perl syntax:

print <<HERE_IT_ENDS;
<a href="$url">$text</a>

C# Handling

The C# programming language handles LTS by the use of the '@' symbol at the start of string literals, before the initial quotation marks e.g.

string filePath = @"C:\Foo\Bar.txt

rather than otherwise requiring:

string filePath = "C:\\Foo\\Bar.txt

See also


Personal tools

Served in 0.085 secs.