如何在Perl中读取和写入文件

Perl is an ideal language for working with files. It has the basic capability of any shell script and advanced tools, such as regular expressions, that make it useful. In order to work with Perl files, you first need to learn how to read and write to them. Reading a file is done in Perl by opening a filehandle to a specific resource.

Perl是处理文件的理想语言 。 它具有使它有用的任何Shell脚本和高级工具(例如正则表达式)的基本功能。 为了使用Perl文件,您首先需要学习如何读取和写入文件。 在Perl中,通过打开特定资源的文件句柄来读取文件。

在Perl中读取文件 ( Reading a File in Perl )

In order to work with the example in this article, you'll need a file for the Perl script to read. Create a new text document called data.txt and place it in the same directory as the Perl program below.

为了使用本文中的示例,您需要一个文件供Perl脚本读取。 创建一个名为data.txt的新文本文档,并将其放置在与下面的Perl 程序相同的目录中。

In the file itself, just type in a few names — one per line:

在文件本身中,只需输入几个名称-每行输入一个:

When you run the script, the output should be the same as the file itself. The script is simply opening the specified file and looping through it line by line, printing each line as it goes.

运行脚本时,输出应与文件本身相同。 该脚本只是打开指定的文件,并逐行循环浏览,逐行打印。

Next, create a filehandle called MYFILE, open it, and point it at the data.txt file.

接下来,创建一个名为MYFILE的文件句柄,将其打开,然后将其指向data.txt文件。

Then use a simple while loop to automatically read each line of the data file one at a time. This places the value of each line in the temporary variable $_ for one loop.

然后使用简单的while循环一次自动读取数据文件的每一行。 这会将每一行的值放在一个循环的临时变量$ _中。

Inside the loop, use the chomp function to clear off the newlines from the end of each line and then print the value of $_ to show that it was read.

在循环内部,使用chomp函数清除每行末尾的换行符,然后打印$ _的值以表明已读取它。

Finally, close the filehandle to finish out the program.

最后,关闭文件句柄以完成程序。

在Perl中写入文件 ( Writing to a File in Perl )

Take the same data file you worked with while learning to read a file in Perl. This time, you will write to it. To write to a file in Perl, you must open a filehandle and point it at the file you're writing. If you're using Unix, Linux or a Mac, you might also need to double-check your file permissions to see if your Perl script is allowed to write to the data file.

学习在Perl中读取文件时,请使用与您相同的数据文件。 这次,您将写信给它。 要在Perl中写入文件,必须打开文件句柄并将其指向要写入的文件。 如果使用的是Unix,Linux或Mac,则可能还需要仔细检查文件权限,以查看是否允许Perl脚本写入数据文件。

If you run this program and then run the program from the previous section on reading a file in Perl, you'll see that it added one more name to the list.

如果您运行此程序,然后从阅读Perl中的文件的上一部分开始运行该程序,则会看到它在列表中又增加了一个名称。

In fact, every time you run the program, it adds another "Bob" to the end of the file. This is happening because the file was opened in append mode. To open a file in append mode, just prefix the filename with the >> symbol. This tells the open function that you want to write to the file by tacking more onto the end of it.

实际上,每次您运行该程序时,它都会在文件末尾添加另一个“鲍勃”。 之所以发生这种情况,是因为该文件是在追加模式下打开的。 要以附加模式打开文件,只需在文件名前加上>>符号即可。 通过在文件末尾添加更多内容,可以告诉open函数您要写入文件。

If instead, you want to overwrite the existing file with a new one, you use the > single greater than symbol to tell the open function that you want a fresh file each time. Try replacing the >> with a > and you see that the data.txt file is cut down to a single name — Bob — each time you run the program.

相反,如果要用一个新文件覆盖现有文件,则使用>大于号来表示打开函数每次都需要一个新文件。 尝试用>代替>>,您会发现每次运行程序时data.txt文件都被缩减为一个名称-Bob。

Next, use the print function to print the new name to the file. You print to a filehandle by following the print statement with the filehandle.

接下来,使用打印功能将新名称打印到文件中。 通过在print语句后加上文件句柄,可以打印到文件句柄。

Finally, close the filehandle to finish out the program.

最后,关闭文件句柄以完成程序。

翻译自: https://www.thoughtco.com/read-and-write-files-in-perl-2641155

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值