大头
Table_bottom

标签云
Table_bottom

分类
Table_bottom

日历
二月
28293031123
45678910
11121314151617
18192021222324
252627282912
Table_bottom

评论
Table_bottom

留言
Table_bottom

微博
Table_bottom

热门文章
Table_bottom

随机文章
Table_bottom

豆瓣上谁关注这里
Table_bottom

链接
Table_bottom

搜索栏
Table_bottom

RSS
RSS Link
Table_bottom

功能
Table_bottom

页面
Table_bottom

计数器
464679
Table_bottom

访客统计
Table_bottom

存档
Table_bottom

初学Perl之三

loveisbug posted @ 2008年11月14日 06:27 in 娱乐 with tags Perl , 3403 阅读

今天程序终于跑对了。程序如下:

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $subset = "书名:</td>";
  6. my $modflag = 0;
  7.  
  8. while(<>)
  9. {
  10.         if($modflag == 1)
  11.         {
  12.                 #get the book name.
  13.                 print $1 if $_ =~ />(.*)<\/a>/m;
  14.                 print "\n";
  15.         }
  16.  
  17.         my $offset = 0;
  18.         $offset = index($_, $subset);
  19.         if($offset == -1)
  20.         {
  21.                 $modflag = 0;
  22.         }
  23.         else
  24.         {
  25.                 $modflag = 1;
  26.         }
  27. }

get the book name那里(L13, L14),这样写也可以:

  1.         $_ =~ m!>(.*?)</a>!;
  2.         print $1, "\n";

这样写的时候,'/a'前面加'\'或者不加'\'结果都是对的;而采用第一种写法时不是这样,必须加'\',去掉则不正确,报错信息是:

Bareword found where operator expected at xxxx line 23, near "/>(.*?)</a"

(Missing operator before a?)

syntax error at xxxx line 23, near "/>(.*?)</a"

这是为什么?

birdinforest 说:
2008年11月21日 05:05

因为容易与'/a'前面的'/'混淆


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter