2013-07-17 103 views

回答

3

如果要按字面順序匹配變量的內容,請將其包含在\Q...\E引號中,或使用quotemeta函數。這兩項工作:

use feature 'say'; 
my $metachars = "1+2"; 
my $escaped_metachars = quotemeta $metachars; 

local $_ = "1112, 1+2"; 

say "\\Q...\\E: ", /(\Q$metachars\E)/; 
say "quotemeta: ", /($escaped_metachars)/; 
say "no escaping: ", /($metachars)/; 

輸出:

\Q...\E: 1+2 
quotemeta: 1+2 
no escaping: 1112 
相關問題