Scalar Data

Double-quoted strings

When strings are enclosed by double quotes, several things happen:

  1. The combination of a backslash and certain character(s) has special meaning (backslash escape). Examples:
    	\n	newline
    	\t	tab
    	\a	bell
        
    Try running this small program table.pl
    #!/usr/local/bin/perl
    
    $table = "ITEM1\tITEM2\tITEM3\nITEM4\tITEM5\tITEM6\n";
    print $table;
    
    $bell = "\a";
    print $bell;
    
  2. Variable interpolation: Variable names inside the string are replaced with their current values. (previous example.)

    Example (students.pl):

    #!/usr/bin/perl
    
    $a = "Perl Programming";
    $b = 11;
    
    $line = "Total of $b students are registered to the $a course.";
    print "$line\n";
    
  3. If you wish to have a literal double quote inside your string, precede it with a backslash (\").

    Same thing if you want to have a literal backslash (\\).

    Examples:

    1. Let's surround the course name with double quotes (students1.pl):

    #!/usr/bin/perl
    
    $a = "Perl Programming";
    $b = 72;
    
    $line = "Total of $b students are registered to the \"$a\" course.";
    print "$line\n";
    

    2. Let's use a backslash when printing a file path in a PC (filepath.pl):

    #!/usr/bin/perl
    
    $file_path = "samson\\presentations\\perl1.ppt";
    
    print "Please find file in: $file_path\n";
    

Table of Contents.
Previous. Next.