Perl 使用用户输入名称重命名文件

Perl 使用用户输入名称重命名文件,perl,perl-module,Perl,Perl Module,我想将我的临时文件“file3.c”重命名为用户输入文件名。 使用文件中的重命名或移动命令::copy不会重命名它 use strict; use warnings; use File::Copy; #input header file print "Input file1:\n"; $input = <>; open(FILE1, $input) || die "couldn't open the file!"; open(FILE3, '>>file3.c') |

我想将我的临时文件“file3.c”重命名为用户输入文件名。 使用文件中的重命名或移动命令::copy不会重命名它

use strict;
use warnings;
use File::Copy;

#input header file
print "Input file1:\n";
$input = <>;
open(FILE1, $input) || die "couldn't open the file!";

open(FILE3, '>>file3.c') || die "couldn't open the file!";
...
#some work on file3.c
...

close(FILE1); 
close(FILE3);

#renaming prepended temporary file name to original file name
rename("file3.c", "$input");
使用严格;
使用警告;
使用文件::复制;
#输入头文件
打印“输入文件1:\n”;
$input=;
打开(FILE1,$input)| | die“无法打开文件!”;
打开(FILE3,'>>FILE3.c')| | die“无法打开文件!”;
...
#关于file3.c的一些工作
...
关闭(文件1);
关闭(文件3);
#将前置临时文件名重命名为原始文件名
重命名(“file3.c”,即“$input”);
输出 不进行重命名


如何重命名它?

您可能只需要
chomp
输入即可删除换行符:

chomp(my $input = <>);
此外,您应该经常使用
而不是
| |
,因为
| |
具有更高的优先级。例如,这是一个很难发现的初学者常见错误:

open my $fh, "<", $file || die $!;  # WRONG!

打开我的$fh,“你可能只需要
咀嚼你的输入就可以删除换行符:

chomp(my $input = <>);
此外,您应该经常使用
而不是
| |
,因为
|
具有更高的优先级。例如,这是初学者常见的错误,很难发现:

open my $fh, "<", $file || die $!;  # WRONG!
打开我的$fh,”