未在perl中复制文件
文件“/root/actual”没有通过perl脚本使用“/root/temp”的内容进行重写。如果手动编辑,将修改“/root/actual”未在perl中复制文件,perl,unix,file-io,Perl,Unix,File Io,文件“/root/actual”没有通过perl脚本使用“/root/temp”的内容进行重写。如果手动编辑,将修改“/root/actual” copy("/root/actual","/root/temp") or die "Copy failed: $!"; open(FILE, "</root/temp") || die "File not found"; my @lines = <FILE>; close(FILE); my @newlines; foreach
copy("/root/actual","/root/temp") or die "Copy failed: $!";
open(FILE, "</root/temp") || die "File not found";
my @lines = <FILE>;
close(FILE);
my @newlines;
foreach(@lines) {
$_ =~ s/$aref1[0]/$profile_name/;
push(@newlines,$_);
}
open(FILE, ">/root/actual") || die "File not found";
print FILE @newlines;
close(FILE);
copy(“/root/actual”,“/root/temp”)或die“复制失败:$!”;
打开(文件“/root/actual”)| | die“未找到文件”;
打印文件@换行符;
关闭(文件);
文件“/root/actual”没有通过perl脚本使用“/root/temp”的内容进行重写。如果手动编辑,将修改“/root/actual”
copy("/root/actual","/root/temp") or die "Copy failed: $!";
open(FILE, "</root/temp") || die "File not found";
my @lines = <FILE>;
close(FILE);
my @newlines;
foreach(@lines) {
$_ =~ s/$aref1[0]/$profile_name/;
push(@newlines,$_);
}
open(FILE, ">/root/actual") || die "File not found";
print FILE @newlines;
close(FILE);
你是说/root/temp
没有被/root/actual
取代吗?或者,/root/temp
正在按照您的意愿进行修改,但它没有在程序末尾复制/root/acutual
我建议您仔细阅读现代Perl编程实践。您需要有使用警告代码>和使用严格代码>在您的程序中。事实上,这个论坛上的许多人不会费心回答Perl问题,除非使用strict代码>和使用警告代码>已使用
$aref1[0]
从哪里来?我看不到在您的程序中任何地方声明了@aref1
。或者,就此而言,$profile\u name
如果要将整个文件读入正则表达式,则没有理由先将其复制到临时文件
我用更现代的语法改写了你的作品:
use strict;
use warnings;
use autodie;
use constant {
FILE_NAME => 'test.txt',
};
my $profile_name = "bar"; #Taking a guess
my @aref1 = qw(foo ??? ??? ???); #Taking a guess
open my $input_fh, "<", FILE_NAME;
my @lines = <$input_fh>;
close $input_fh;
for my $line ( @lines ) {
$line =~ s/$aref1[0]/$profile_name/;
}
open my $output_fh, ">", FILE_NAME;
print ${output_fh} @lines;
close $output_fh;
使用严格;
使用警告;
使用自动模具;
使用常数{
文件名=>'test.txt',
};
我的$profile_name=“bar”#猜测
my@aref1=qw(foo??)#猜测
打开我的$input\u fh,“,文件名;
打印${output_fh}@行;
关闭$output\u fh;
这很有效
笔记:
使用autodie代码>表示您不必检查文件是否已打开
当我对
循环使用时,我可以在数组中进行就地替换。每个项都是指向数组中该项的指针
无需复制
或临时文件,因为您正在替换原始文件
我没有在这里使用它,因为您没有,但是map{s/$aref1[0]/$profile\u name/}@lines
可以为
循环替换该。看
是否正在执行die“未找到文件”
命令?或者,脚本运行了,但什么也没发生?这是您的整个脚本吗?@aref1
在哪里设置?其中是use File::Copy代码>?使用的文件::也复制。没有执行死亡命令。你能发布你的整个脚本吗?那将非常有帮助。您应该始终使用警告代码>和使用严格在Perl程序中使用code>。你还需要阅读更现代的语法。另外,让我们知道您的脚本生成的任何输出。我们需要更多的解释,而不是“它不起作用”。