无法使用包含单引号的键从存储中检索数据

时间:2019-06-17 18:19:33

标签: perl search catalyst

数据库包含公司名称,名称中包含单引号。当用户选择公司时,我需要使用key = CompanyName从哈希中检索。我遇到的问题是无法从存储中检索内容。

在调试模式下,我已验证以下内容,能够在公司名称周围使用单引号和双引号:

  DB<12> x $c->stash->{'company_list'}->{'corp_summary'}->{'Waldo\\\'s Merchandising Services, LLC'}
0  'c635|i1193|i1194|i1220|i1221|'
  DB<13> x $c->stash->{'company_list'}->{'corp_summary'}->{"Waldo\\\'s Merchandising Services, LLC"}
0  'c635|i1193|i1194|i1220|i1221|'

该代码会打印出我要查找的名称和已经完成的替换,但是最后一行代码“ $ codes”仍然为空。

306       print "$cc\n";
307       print "$cc\n";
308       print "$cc\n";
309       print "$cc\n";
310       my $temp = $cc;
311       $temp =~ s/'/\/\/\/\'/g;
312       
313       print "$temp\n";
314       print "$temp\n";
315       print "$temp\n";
316       print "$temp\n";
317       print "$temp\n";
318       my $codes = $c->stash->{'company_list'}->{'corp_summary'}->{"$temp"};

打印语句的输出:

Waldo's Merchandising Services, LLC
Waldo's Merchandising Services, LLC
Waldo's Merchandising Services, LLC
Waldo's Merchandising Services, LLC
Waldo///'s Merchandising Services, LLC
Waldo///'s Merchandising Services, LLC
Waldo///'s Merchandising Services, LLC
Waldo///'s Merchandising Services, LLC
Waldo///'s Merchandising Services, LLC

我的预期输出是使$ codes包含“ c635 | i1193 | i1194 | i1220 | i1221 |”

1 个答案:

答案 0 :(得分:3)

您使用的是正斜杠而不是反斜杠,并且比$temp中需要的斜杠多两个。试试:

$temp =~ s{'}{\\'}g;