Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between single and double quotes in Perl?

Tags:

In Perl, what is the difference between ' and " ?

For example, I have 2 variables like below:

$var1 = '\('; $var2 = "\(";  $res1 = ($matchStr =~ m/$var1/); $res2 = ($matchStr =~ m/$var2/); 

The $res2 statement complains that Unmatched ( before HERE mark in regex m.

like image 910
chappar Avatar asked Jun 03 '09 09:06

chappar


People also ask

What is the difference between single quote and double quote?

The main difference between double quotes and single quotes is, double quotes are used to denote a speech or a quotation whereas single quotes are used to indicate quote within a quotation.

What is the difference between single and double-quoted string?

A single-quoted string does not have variables within it interpreted. A double-quoted string does. Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.

Why use single quotes instead of double quotes?

The most common reason to use single quotation marks is to quote someone who is quoting someone else. The rules are different in British English, but in American English, you enclose the primary speaker's comments in double quotation marks, and then you enclose the thing they are quoting in single quotation marks.


1 Answers

Double quotes use variable expansion. Single quotes don't

In a double quoted string you need to escape certain characters to stop them being interpreted differently. In a single quoted string you don't (except for a backslash if it is the final character in the string)

my $var1 = 'Hello';  my $var2 = "$var1"; my $var3 = '$var1';  print $var2; print "\n"; print $var3; print "\n"; 

This will output

Hello $var1 

Perl Monks has a pretty good explanation of this here

like image 177
Xetius Avatar answered Sep 20 '22 18:09

Xetius