当前位置:  开发笔记 > 编程语言 > 正文

perl脚本的限制效果

如何解决《perl脚本的限制效果》经验,为你挑选了1个好方法。

我想在tex文档上运行perl脚本,但不在序言中运行.如何限制它对某个模式下的文件部分的影响(例如^\\begin\{document\}$)?这是脚本:

# Insert the macro \gr{} around Greek passages.

#!/usr/bin/env perl
use strict;
use warnings;
use Encode;

my $L = qr/[^A-Za-z]/;
my $g = qr/\p{Greek}/;

local $/;           # slurp
$_ = decode('utf-8', <>);

# Remove already existing instances.
s/\\gr
(               # 1
 {
  (             # 2
   (?: \\.          # 3. escaped chars
     | [^{}]
     | (?1)         # recur to 1
   )*
  )  
 }
)
/$2/xg;

# Insert new.
s/(
  [([]*             # begin with puncuation?
  $g                # Greek;
  ($L|\\\w+)*       # contain any non-Latin char or cmd;
  $g                # end with Greek
  [)\]]*            # and puncuation?
  )
/\\gr{$&}/xg;

print encode('utf-8', $_);

7stud.. 6

local $/可用于除完全啜食之外的其他事物. $/是输入记录分隔符,perl读取所有内容,包括输入记录分隔符,然后将其作为一行返回.默认值$/是换行符"\n".

如果将输入记录分隔符设置为undef,那么(某种程度上)perl将永远不会在文件中找到输入记录分隔符,因此您将整个文件作为一行返回.但您可以将输入记录分隔符设置为您想要的任何内容...

$ cat data.txt
I don't want to proccess 
this part of the file.
\begin{document}
I just want to process
the stuff down here.
\begin{document}
hello

use strict;
use warnings; 
use 5.020;
use autodie;
use Data::Dumper;

my $fname = 'data.txt';
open my $INFILE, '<', $fname;

my ($unprocessed, $needs_processing);

{
    local $/ = "\\begin{document}\n";
    $unprocessed = <$INFILE>;
    $/ = undef;  #Read rest of file no matter what it contains.
    $needs_processing = <$INFILE>;
}

close $INFILE;

print $unprocessed;
say '-' x 10;
print $needs_processing;

--output:--
I don't want to proccess 
this part of the file.
\begin{document}
----------
I just want to process
the stuff down here.
\begin{document}
hello

如果要对文件进行原位编辑:

use strict;
use warnings; 
use 5.020;
use autodie;
use Data::Dumper;

my $fname = 'data.txt';
my $divider = "\\begin{document}\n";
my $backup = '.bak';

open my $INFILE, '<', $fname;

{
    local ($^I, $/, @ARGV) = ($backup, $divider, $fname);

    CHUNK:
    while(<>) {

        if($. == 1) {    # $. is the line number (starts at 1)
            print;       #STDOUT has been redirected to the file 'data.txt'.
            $/ = undef;  #Read rest of file no matter what it contains.
            next CHUNK;
        }

        #Process $_ here:
        s/e/E/g;

        print;  #STDOUT has been redirected to the file 'data.txt'.
    }

}

close $INFILE;

$ cat data.txt
I don't want to proccess 
this part of the file.
\begin{document}
I just want to procEss
thE stuff down hErE.
\bEgin{documEnt}
hEllo

原始文件将在data.txt.bak.如果您不想备份,请为其分配空白字符串$^I.

请注意,在您的代码中,语句:

local $/;

没有做任何有用的事情.在您的代码中,该语句不在块内(=由大括号包围的代码的一部分). local $/说:

    存放$/某处的原始值.

    将undef分配给$/.

    local $/退出包含的块时,将原始值指定给$/.

但是因为local $/;不在代码中的块内,所以不会退出任何块,并且$/永远不会恢复原始值.因此,隐藏原始价值毫无意义$/.



1> 7stud..:

local $/可用于除完全啜食之外的其他事物. $/是输入记录分隔符,perl读取所有内容,包括输入记录分隔符,然后将其作为一行返回.默认值$/是换行符"\n".

如果将输入记录分隔符设置为undef,那么(某种程度上)perl将永远不会在文件中找到输入记录分隔符,因此您将整个文件作为一行返回.但您可以将输入记录分隔符设置为您想要的任何内容...

$ cat data.txt
I don't want to proccess 
this part of the file.
\begin{document}
I just want to process
the stuff down here.
\begin{document}
hello

use strict;
use warnings; 
use 5.020;
use autodie;
use Data::Dumper;

my $fname = 'data.txt';
open my $INFILE, '<', $fname;

my ($unprocessed, $needs_processing);

{
    local $/ = "\\begin{document}\n";
    $unprocessed = <$INFILE>;
    $/ = undef;  #Read rest of file no matter what it contains.
    $needs_processing = <$INFILE>;
}

close $INFILE;

print $unprocessed;
say '-' x 10;
print $needs_processing;

--output:--
I don't want to proccess 
this part of the file.
\begin{document}
----------
I just want to process
the stuff down here.
\begin{document}
hello

如果要对文件进行原位编辑:

use strict;
use warnings; 
use 5.020;
use autodie;
use Data::Dumper;

my $fname = 'data.txt';
my $divider = "\\begin{document}\n";
my $backup = '.bak';

open my $INFILE, '<', $fname;

{
    local ($^I, $/, @ARGV) = ($backup, $divider, $fname);

    CHUNK:
    while(<>) {

        if($. == 1) {    # $. is the line number (starts at 1)
            print;       #STDOUT has been redirected to the file 'data.txt'.
            $/ = undef;  #Read rest of file no matter what it contains.
            next CHUNK;
        }

        #Process $_ here:
        s/e/E/g;

        print;  #STDOUT has been redirected to the file 'data.txt'.
    }

}

close $INFILE;

$ cat data.txt
I don't want to proccess 
this part of the file.
\begin{document}
I just want to procEss
thE stuff down hErE.
\bEgin{documEnt}
hEllo

原始文件将在data.txt.bak.如果您不想备份,请为其分配空白字符串$^I.

请注意,在您的代码中,语句:

local $/;

没有做任何有用的事情.在您的代码中,该语句不在块内(=由大括号包围的代码的一部分). local $/说:

    存放$/某处的原始值.

    将undef分配给$/.

    local $/退出包含的块时,将原始值指定给$/.

但是因为local $/;不在代码中的块内,所以不会退出任何块,并且$/永远不会恢复原始值.因此,隐藏原始价值毫无意义$/.

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有