Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
834 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 "
";
print $var3;
print "
";

This will output

Hello
$var1

Perl Monks has a pretty good explanation of this here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...