Leaning toothpick syndrome

From Wikipedia, the free encyclopedia

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 into wide usage; there, the phrase is used to describe regular expressions which match Unix-style path names, in which elements are separated by forward slashes.

LTS appears in many programming languages and in many situations, including:

Contents

[edit] Pattern example

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

m/ftp:\/\/[^\/]*\/pub\//

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:

m{ftp://[^/]*/pub/}
m#ftp://[^/]*/pub/#
m!ftp://[^/]*/pub/!

[edit] HTML example

A program is supposed to output an HTML-Link, the URL and the text for the link are found in variables $url and $text respectively:

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

Using single quotes to delimit the string is not feasible: the variables canot be used inside single quotes:

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

Using the funktion printf 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>'/;

The here-doc is especially well suited for multiline strings and not suited for proper Indentation. This example shows the Perl-Syntax

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

The same example in PHP-Syntax:

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

[edit] References

[edit] See also