layout: post
title: String literals
-
7 November 2011
there are many possible choices here, the wikipedia article on strings explains the many notations for a string literal ; here documents – multiple line strings explains more about strings that span multiple lines;
Perl achieves a great degree of expressiveness at the cost of introducing multiple kinds of string literals.
Perl has
I guess it is a bit complicated to memorize so many notations, especially if one is new to programming.
There are therefore the following requirements for a string notation.
Escape sequences can be avoided, if the definition of the HERE document contains a definition of the end of string delimiter;
‘this is a string’
‘this is a string
that spans two lines’
Or it can start with multiple ’ characters, and must end with the same number of characters.
‘’this is a string too, now I can have a single ’ character in the text of the string’’
Also one can have an expression embedded in the string
‘value of factorial of 10 is [ fact( 10 ) ] a big number’
Here the text within the brackets [ ] is computed, the result of this computation is inserted into the string.
Now if we have multiple ’ characters for start of string delimiter, then we have multiple brackets as start of expression delimiter.
‘’value of factorial of 10 is [[ fact( 10 ) ]] a big number’’
This construct is powerful but a bit ambiguous; How do you do an empty string?
Now we can introduce the " – which will be an empty string.
Or you can call a library function emptystring() which returns an empty string.
Often one wants to print a text string followed by a new line; in Perl one does not need a separate println function – it is easy to express a string constant that has a newline at the end; in Pascal one does not have escape sequences, therefore they added the println function that writes a newline.
Also you can have a libraryfunction newline(), which returns a single newline character;