⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06_23.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
        eval {                    # try to look up escapes for stand-out            require POSIX;        # or underline via Term::Cap            use Term::Cap;            my $termios = POSIX::Termios-&gt;new();            $termios-&gt;getattr;            my $ospeed = $termios-&gt;getospeed;            $terminal = Tgetent Term::Cap { TERM=&gt;undef, OSPEED=&gt;$ospeed }        };        unless ($@) {             # if successful, get escapes for either            local $^W = 0;        # stand-out (-H) or underlined (-u)            ($SO, $SE) = $opt{H}                ? ($terminal-&gt;Tputs('so'), $terminal-&gt;Tputs('se'))                : ($terminal-&gt;Tputs('us'), $terminal-&gt;Tputs('ue'));        }        else {                    # if use of Term::Cap fails,            ($SO, $SE) = $opt{H}  # use tput command to get escapes                ? (`tput -T $term smso`, `tput -T $term rmso`)                : (`tput -T $term smul`, `tput -T $term rmul`)        }    }    if ($opt{i}) {        @patterns = map {&quot;(?i)$_&quot;} @patterns;    }    if ($opt{p} || $opt{P}) {        @patterns = map {&quot;(?m)$_&quot;} @patterns;    }    $opt{p}   &amp;&amp; ($/ = '');    $opt{P}   &amp;&amp; ($/ = eval(qq(&quot;$opt{P}&quot;)));     # for -P '%%\n'    $opt{w}   &amp;&amp; (@patterns = map {'\b' . $_ . '\b'} @patterns);    $opt{'x'} &amp;&amp; (@patterns = map {&quot;^$_\$&quot;} @patterns);    if (@ARGV) {        $Mult = 1 if ($opt{r} || (@ARGV &gt; 1) || -d $ARGV[0]) &amp;&amp; !$opt{h};    }    $opt{1}   += $opt{l};                   # that's a one and an ell    $opt{H}   += $opt{u};    $opt{c}   += $opt{C};    $opt{'s'} += $opt{c};    $opt{1}   += $opt{'s'} &amp;&amp; !$opt{c};     # that's a one    @ARGV = ($opt{r} ? '.' : '-') unless @ARGV;    $opt{r} = 1 if !$opt{r} &amp;&amp; grep(-d, @ARGV) == @ARGV;    $match_code  = '';    $match_code .= 'study;' if @patterns &gt; 5; # might speed things up a bit    foreach (@patterns) { s(/)(\\/)g }    if ($opt{H}) {        foreach $pattern (@patterns) {            $match_code .= &quot;\$Matches += s/($pattern)/${SO}\$1${SE}/g;&quot;;        }    }    elsif ($opt{v}) {        foreach $pattern (@patterns) {            $match_code .= &quot;\$Matches += !/$pattern/;&quot;;        }    }    elsif ($opt{C}) {        foreach $pattern (@patterns) {            $match_code .= &quot;\$Matches++ while /$pattern/g;&quot;;        }    }    else {        foreach $pattern (@patterns) {            $match_code .= &quot;\$Matches++ if /$pattern/;&quot;;        }    }    $matcher = eval &quot;sub { $match_code }&quot;;    die if $@;    return (\%opt, $matcher);}###################################sub matchfile {    $opt = shift;                 # reference to option hash    $matcher = shift;             # reference to matching sub    my ($file, @list, $total, $name);    local($_);    $total = 0;FILE: while (defined ($file = shift(@_))) {        if (-d $file) {            if (-l $file &amp;&amp; @ARGV != 1) {                warn &quot;$Me: \&quot;$file\&quot; is a symlink to a directory\n&quot;                    if $opt-&gt;{T};                next FILE;            }            if (!$opt-&gt;{r}) {                warn &quot;$Me: \&quot;$file\&quot; is a directory, but no -r given\n&quot;                    if $opt-&gt;{T};                next FILE;            }            unless (opendir(DIR, $file)) {                unless ($opt-&gt;{'q'}) {                    warn &quot;$Me: can't opendir $file: $!\n&quot;;                    $Errors++;                }                next FILE;            }            @list = ();            for (readdir(DIR)) {                push(@list, &quot;$file/$_&quot;) unless /^\.{1,2}$/;            }            closedir(DIR);            if ($opt-&gt;{t}) {                my (@dates);                for (@list) { push(@dates, -M) }                @list = @list[sort { $dates[$a] &lt;=&gt; $dates[$b] } 0..$#dates];            }            else {                @list = sort @list;            }            matchfile($opt, $matcher, @list);    # process files            next FILE;        }        if ($file eq '-') {            warn &quot;$Me: reading from stdin\n&quot; if -t STDIN &amp;&amp; !$opt-&gt;{'q'};            $name = '&lt;STDIN&gt;';        }        else {            $name = $file;            unless (-e $file) {                warn qq($Me: file &quot;$file&quot; does not exist\n) unless $opt-&gt;{'q'};                $Errors++;                next FILE;            }            unless (-f $file || $opt-&gt;{a}) {                warn qq($Me: skipping non-plain file &quot;$file&quot;\n) if $opt-&gt;{T};                next FILE;            }            my ($ext) = $file =~ /\.([^.]+)$/;            if (defined $ext &amp;&amp; exists $Compress{$ext}) {                $file = &quot;$Compress{$ext} &lt;$file |&quot;;            }            elsif (! (-T $file  || $opt-&gt;{a})) {                warn qq($Me: skipping binary file &quot;$file&quot;\n) if $opt-&gt;{T};                next FILE;            }        }        warn &quot;$Me: checking $file\n&quot; if $opt-&gt;{T};        unless (open(FILE, $file)) {            unless ($opt-&gt;{'q'}) {                warn &quot;$Me: $file: $!\n&quot;;                $Errors++;            }            next FILE;        }        $total = 0;        $Matches = 0;LINE:  while (&lt;FILE&gt;) {            $Matches = 0;                ##############            &amp;{$matcher}();        # do it! (check for matches)            ##############            next LINE unless $Matches;            $total += $Matches;            if ($opt-&gt;{p} || $opt-&gt;{P}) {                s/\n{2,}$/\n/ if $opt-&gt;{p};                chomp         if $opt-&gt;{P};            }            print(&quot;$name\n&quot;), next FILE if $opt-&gt;{l};            $opt-&gt;{'s'} || print $Mult &amp;&amp; &quot;$name:&quot;,                $opt-&gt;{n} ? &quot;$.:&quot; : &quot;&quot;,                $_,                ($opt-&gt;{p} || $opt-&gt;{P}) &amp;&amp; ('-' x 20) . &quot;\n&quot;;            next FILE if $opt-&gt;{1};                 # that's a one        }    }    continue {        print $Mult &amp;&amp; &quot;$name:&quot;, $total, &quot;\n&quot; if $opt-&gt;{c};    }    $Grand_Total += $total;}<ACLASS="indexterm"NAME="ch06-idx-1000007737-0"></A><ACLASS="indexterm"NAME="ch06-idx-1000007737-1"></A><ACLASS="indexterm"NAME="ch06-idx-1000007737-2"></A></PRE></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_22.htm"TITLE="6.21. Program: urlify"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 6.21. Program: urlify"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch06_24.htm"TITLE="6.23. Regular Expression Grabbag"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 6.23. Regular Expression Grabbag"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">6.21. Program: urlify</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">6.23. Regular Expression Grabbag</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -