在perl中创建一个文本文件

Ari*_*ule 1 perl

我希望得到一些帮助,如何在perl中专门创建一个接收用户输入的文本文件.

print "\ndirectory has been changed";
print "\nDear user, please enter the name of the file you want to create ";

my $nameFile = <>;

open MYFILE, ">$nameFile" or die "couldn't open the file $!";
Run Code Online (Sandbox Code Playgroud)

这将创建一个文件,但不是文本文件.

关于阿里安

TLP*_*TLP 6

更新:我刚刚意识到你没有chompSTDIN,所以你的文件名末尾附有换行符.

chomp $filename;
Run Code Online (Sandbox Code Playgroud)

应该用你的文件名解决那个特殊的打嗝.

推荐使用的方法open是使用三个参数和一个词法文件句柄.MYFILE是一个全球性的,具有所有固有的缺点和好处.

use autodie;  # will check important errors for you, such as open
open my $fh,      '>',               $namefile;
#    ^- lexical   ^- explicit mode   ^- filename separated from mode

print $fh "This is a textfile\n";   # put something in the file
Run Code Online (Sandbox Code Playgroud)