使用 Perl 的菱形和就地编辑运算符对目录中的多个文件进行就地编辑 [英] In-place editing of multiple files in a directory using Perl's diamond and in-place edit operator
问题描述
我正在尝试使用 Perl 的就地编辑运算符 $^I 就地编辑一堆文本文件.我使用菱形 (<>) 运算符遍历目录,如下所示:
I am trying to in-place edit a bunch of text files using Perl's in-place edit operator $^I. I traverse through the directory using the diamond (<>) operator like this:
$^I = ".bak";
@ARGV = <*.txt>;
while (<>)
{
s/((?:^|\s)-?)(0+)(?=\s|$)/$1.$2/g;
print;
}
这完美地工作并且完成了我需要它做的工作.但是,如果我的 @ARGV 已经填充了程序所需的其他一些数据怎么办?我尝试执行以下操作:
This works perfectly and does the job I need it to do. But what if my @ARGV is already populated with some other data I need for the program? I tried to do the following:
$^I = ".bak";
my @files = <*.txt>;
while (<@files>)
{
s/((?:^|\s)-?)(0+)(?=\s|$)/$1.$2/g;
print;
}
但它不起作用.我在这里想念什么?我不能使用我的 $ARGV,因为它包含其他数据并且不能将它与文件匹配模式混淆.
But it does not work. What I am I missing here? I can't use my $ARGV as it contains other data and can't mess it with file matching patterns.
有什么建议吗?
谢谢!
推荐答案
您可以事先复制您的参数,然后使用 @ARGV
.例如:
You can copy your arguments beforehand, and then use @ARGV
. E.g.:
my @args = @ARGV;
@ARGV = <*.txt>;
由于有一些隐藏的处理正在进行,这是特定于 @ARGV
和菱形运算符 <>
的使用,你不能只使用另一个数组做同样的事情.
Since there is some hidden processing going on, which is specific to the use of @ARGV
and the diamond operator <>
, you cannot just use another array to do the same thing.
这篇关于使用 Perl 的菱形和就地编辑运算符对目录中的多个文件进行就地编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!