String Literal - Variable Interpolation

Variable Interpolation

Languages differ on whether and how to interpret string literals as either 'raw' or 'variable interpolated'. Variable interpolation is the process of evaluating an expression containing one or more variables, and returning output where the variables are replaced with their corresponding values in memory. In sh-compatible Unix shells, quotation-delimited (") strings are interpolated, while apostrophe-delimited (') strings are not.

For example, the following Perl code:

$name = "Nancy"; $greeting = "Hello World"; print "$name said $greeting to the crowd of people.";

produces the output:

Nancy said Hello World to the crowd of people.

The sigil character ($) is interpreted to indicate variable interpolation.

Similarly, the printf function produces the same output using notation such as:

printf "%s said %s to the crowd of people.", $name, $greeting;

The metacharacters (%s) indicate variable interpolation.

This is contrasted with "raw" strings:

print '$name said $greeting to the crowd of people.';

which produce output like:

$name said $greeting to the crowd of people.

Here the $ characters are not sigils, and are not interpreted to have any meaning other than plain text.

Read more about this topic:  String Literal

Famous quotes containing the word variable:

    Walked forth to ease my pain
    Along the shore of silver streaming Thames,
    Whose rutty bank, the which his river hems,
    Was painted all with variable flowers,
    Edmund Spenser (1552?–1599)