Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 搜索和替换未保存到文件_Perl_Search_Replace - Fatal编程技术网

Perl 搜索和替换未保存到文件

Perl 搜索和替换未保存到文件,perl,search,replace,Perl,Search,Replace,我是新来的Perl,已经用它工作了一天了。我正在尝试编写一个脚本,该脚本将访问每个.cpp文件和.hpp文件,更改文件的读写权限,同时搜索并替换字符串。这就是我目前所拥有的。我可以更改每个文件的读写权限问题是当我尝试替换字符串时。它打印正确,但不会保存到文件中。欢迎提出任何建议 #gets first first value of array being past as argument. my $path = shift; #open directory opendir(DIR, $pat

我是新来的Perl,已经用它工作了一天了。我正在尝试编写一个脚本,该脚本将访问每个.cpp文件和.hpp文件,更改文件的读写权限,同时搜索并替换字符串。这就是我目前所拥有的。我可以更改每个文件的读写权限问题是当我尝试替换字符串时。它打印正确,但不会保存到文件中。欢迎提出任何建议

#gets first first value of array being past as argument. 
my $path = shift;

#open directory
opendir(DIR, $path) or die "Unable to open $path: $!";
#read in the files
#ignores hidden files eg. .\..\
my @files = grep{!/^\.{1,2}$/} readdir(DIR);
#close directory
close(DIR);
#put full path of file
@files = map {$path . '\\' . $_ } @files;

for (@files){
    #if directory then use recusrion to open file 
    if(-d $_){
        change_permission($_);
    }elsif((-f $_) && (($_ =~m/\.cpp/) || ($_ =~m/\.hpp/) || ($_ =~m/\.txt/))){
        chmod 0666, $_ or die "Couldn't chmod";

        open(DATA, "+<", $_) or die "file could not open $! \n";
            while(<DATA>){
                s/best/worst/ig;
                print;
            }

        close(DATA) or die "Couldn't close file properly $! \n" ;


    }
}
#获取作为参数通过的数组的第一个值。
我的$path=shift;
#开放目录
opendir(DIR,$path)或die“无法打开$path:$!”;
#读入文件
#忽略隐藏的文件,例如\\
my@files=grep{!/^\.{1,2}$/}readdir(DIR);
#关闭目录
关闭(DIR);
#放置文件的完整路径
@files=map{$path.\\'.$\}@files;
对于(@文件){
#如果是目录,则使用recusrion打开文件
如果(-d$){
更改权限($);
}elsif((-f$\\.cpp/)&($\=~m/\.cpp/)|($\=~m/\.hpp/)|($\=~m/\.txt/)){
chmod 0666,$或die“无法chmod”;

使用
print;
时打开(数据,“+您正在打印到
STDOUT
,因为这是所选的文件句柄。您的输出将在屏幕上可见,但不会在文件中更改。您打开它进行读写的事实在这里并不重要

您可以使用
打印句柄ARGS
表单打印到文件句柄

print DATA $_;

(请注意,
数据
对于句柄来说是一个非常糟糕的主意,因为这是Perl提供的一个默认句柄,用于读取脚本的
\uuuuu数据
部分。一般来说,您应该使用词法文件句柄和三个参数
打开
,因此它将变成
打开我的$fh',+我会使用类似的东西。)

use strict;
use warnings;
use Path::Tiny;

my $p = shift // '.';
my $iter = path($p)->iterator({recurse => 1});
while( my $path = $iter->() ) {
        $path->chmod("ug+w");
        $path->edit( sub { s/best/worst/ } ) if( -f $path && $path =~ /\.([ch]pp|txt)$/i );
}

欢迎使用Stack Overflow和Perl标记。如果除了回答您的问题之外,您还想获得一些关于代码的建设性反馈,请在Perl标记和初学者中随意发布。我看到了一些可以改进的地方。相关的,可能是重复的,但需要对pro进行重大更改gram:简而言之,你的打印到
STDOUT
;这就像
print STDOUT$
。它不能也不应该神奇地更改文件。你应该将新内容写入另一个文件,然后将其移到原始文件上。例如,请查看相关内容,并搜索SO帖子。我建议不要使用
'+'
或附加
'>>“
# elsif (...) {
    chmod 0666, $_ or die "Couldn't chmod";

    @ARGV = ($_);
    while(<DATA>){
        s/best/worst/ig;
        print;
    }
}
use strict;
use warnings;
use Path::Tiny;

my $p = shift // '.';
my $iter = path($p)->iterator({recurse => 1});
while( my $path = $iter->() ) {
        $path->chmod("ug+w");
        $path->edit( sub { s/best/worst/ } ) if( -f $path && $path =~ /\.([ch]pp|txt)$/i );
}