tags:

views:

674

answers:

6

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".

+2  A: 

Double quotation marks interpret, and single quotation do not

Silfverstrom
The idea is right, but you can't use 42isanumber as a variable name.
Igor Krivokon
yeah that struck me just after i wrote it
Silfverstrom
s/interpret/interpolate/
Sinan Ünür
+3  A: 

' will not resolve variables and escapes

" will resolve variables, and escape characters.

If you want to store your \ character in the string in $var2, use "\\("

Chaos
s/resolve/interpolate/
Sinan Ünür
+13  A: 

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

Xetius
You mean to say that in singe quote '\(' consists of 1 character and in double quote "\(" consists of 2 characters?
chappar
No, it's the opposite. '\(' is 2 characters, and "\(" is one.
Igor Krivokon
The delimiter character must also be backslashed, and backslash in various other cases besides as the final character of the string (e.g. the two character string \' is '\\\'', not '\\'').
ysth
er... the print examples and their output is backwards. print $var2 will print Hello, print $var3 will print $var1.
R. Bemrose
As Xetius mentions, you need to be careful escaping certain characters in interpolating quotes. One of the common misses is the '@' character: if you're stringifying email addresses, for example, be sure to escape the '@' if they'll be inside interpolating quotes.
rpj
Also, I say "interpolating quotes" because in Perl, ' and " aren't the only way to quote strings. q// and qq// are the equivalents of ' and ", respectively, and there are a slew of others: http://perldoc.perl.org/perlop.html#Quote-Like-Operators
rpj
+1  A: 

"" Supports variable interpolation and escaping. so inside "\(" \ escapes (

Where as ' ' does not support either. So '\(' is literally \(

Canopus
+2  A: 

Perl takes the single-quoted strings 'as is' and interpolates the double-quoted strings. Interpolate means, that it substitutes variables with variable values, and also understands escaped characters. So, your "\(" is interpreted as '(', and your regexp becomes m/(/, this is why Perl complains.

Igor Krivokon
In single quotish strings it recognizes two escaped characters: the ending delimiter character and \.
ysth
+2  A: 

If you are going to create regex strings you should really be using the qr// quote-like operator:

my $matchStr = "(";
my $var1 = qr/\(/;
my $res1 = ($matchStr =~ m/$var1/);

It creates a compiled regex that is much faster than just using a variable containing string. It also will return a string if not used in a regex context, so you can say things like

print "$var1\n"; #prints (?-xism:\()
Chas. Owens
The OP may wish to read perlop sections on Quotelike operators: http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators
daotoad
@daotoad You mean like the link in the answer?
Chas. Owens