Cloudbook: C

  1. Home
  2. Variables
  3. Strings
  4. § 3 exercise →
Strings

Perl strings support variable interpolation. If a variable name appears inside a string defined by a pair of double quotes, the value of the string incorporates the values of the variables, not the names of the variables.

If $hello = "Hello", then "$hello World!" will be variable interpolated, and the result will be "Hello World!". However, '$hello World!' will not be interpolated. The single quotes turns off interpolation, and the value of that string is "$hello World!".

If the interpolated variable contains a string, the interpolation is exact. For instance,

        $a = "strange" ;
        print "C'est tres $a.\n" ;
        
will print: "C'est tres strange.", not "C'est tres étrange.", obviously. Nor does:
        $i = 1 ;
        print "$i plus $i equals two.\n" ;
        
print "One plus one equals two.". It prints "1 plus 1 equals two.". Even:
        $i = 1.0000 ;
        print "$i plus $i equals two.\n" ;
        
prints "1 plus 1 equals two.". The five digits of precision implied by writing 1.0000 is not maintained as part of the value.

When a variable is interpolated into a string, Perl converts the value into its choice of a text representation of the value and interpolates that text into the string.