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

后端 未结 6 1287
小蘑菇
小蘑菇 2020-11-30 13:50

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

For example, I have 2 variables like below:

$var1 = \'\\(\';
$var2 = \"         


        
6条回答
  •  爱一瞬间的悲伤
    2020-11-30 14:29

    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:\()
    

提交回复
热议问题