免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1756 | 回复: 5

请教perl 引号问题 [复制链接]

论坛徽章:
0
发表于 2009-10-28 16:46 |显示全部楼层
D:/perl/2.txt:

  1. x.x.x.x        uYu337i\r@06        22        aa        aa        无
  2. x.x.x.x        IM;MZ26py`9D        22        aa        aa        无
  3. x.x.x.x        4&63bA2fqp%^        22        aa        aa        无
  4. x.x.x.x        7=RLf3yBti=&        22        aa        aa        无
  5. x.x.x.x        K=^72sLX8v.J        22        aa        aa        无
  6. x.x.x.x        Qxh41P^=h4"=        22        aa        aa        无
  7. x.x.x.x        ?ba8D14jm?2f        22        aa        aa        无
复制代码

  1. sub insert(){
  2.         open FILE,"D:/perl/2.txt" ;
  3.         while (<FILE>) {
  4.                 my ($ip,$pw,$port,$location,$business,$remark) = split ;
  5.                 print ;
  6.                 my $insert= "insert into ip_info (ip,pw,port,location,business,remark)
  7.                 values ('$ip','\Q$pw\E','$port','$location','$business','$remark')";
  8.                 $s = $dbh->prepare($insert);
  9.                 $s->execute();
  10.         }
  11.         $s->finish();
  12.         $dbh->do("commit");
  13.         close FILE ;
  14. }
复制代码


1.执行这个函数后,在MySQL数据库中4&63bA2fqp%^变成4&63bA2fqp\%^
2.而如果\Q$pw\E'不加\Q.......\E的话uYu337i\r@06就会变成
  1. uYu337i
  2. @06
复制代码


哪位大大帮帮忙了。谢谢!

论坛徽章:
1
技术图书徽章
日期:2014-03-06 15:29:50
发表于 2009-10-28 17:18 |显示全部楼层
quotemeta EXPR
quotemeta
Returns the value of EXPR with all non-"word" characters backslashed. (That is, all characters not matching /[A-Za-z_0-9]/ will be preceded by a backslash in the returned string, regardless of any locale settings.) This is the internal function implementing the \Q escape in double-quoted strings.

论坛徽章:
0
发表于 2009-10-28 17:21 |显示全部楼层
$dbh->quote($string);

or

qq{$string};

论坛徽章:
0
发表于 2009-10-28 17:31 |显示全部楼层
hi histubunnu。
用了你的方法还是有问题:
应该是这样使用吧?
  1. sub insert(){
  2.         open FILE,"D:/perl/2.txt" ;
  3.         while (<FILE>) {
  4.                 my ($ip,$pw,$port,$location,$business,$remark) = split ;
  5.                 print ;
  6.                 $dbh->quote($pw);
  7.                 my $insert= "insert into ip_info (ip,pw,port,location,business,remark)
  8.                 values ('$ip','\Q$pw\E','$port','$location','$business','$remark')";
  9.                 $s = $dbh->prepare($insert);
  10.                 $s->execute();
  11.         }
  12.         $s->finish();
  13.         $dbh->do("commit");
  14.         close FILE ;
  15. }
复制代码

论坛徽章:
0
发表于 2009-10-28 19:29 |显示全部楼层


  1. my $tmpsql = join ",",map{ $dbh -> quote($_) } ($ip,$pw,$port,$location,$business,$remark);

  2. my $sql = "insert into ip_info (ip,pw,port,location,business,remark) VALUES ($tmpsql)";
  3. $dbh->do($sql);

复制代码

论坛徽章:
0
发表于 2009-10-28 20:51 |显示全部楼层
非常感谢,现在可以啦。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP