如何在Perl中添加文件的开头或结尾?

时间:2022-05-10 07:21:09

What is the best way of writing the some content to the beginning and end of a File in Perl

将一些内容写入Perl文件的开头和结尾的最佳方法是什么?

5 个解决方案

#1


Appending is trivial:

追加是微不足道的:

open my $fh, '>>', 'some.file' or die "Cannot open file for append: $!\n";
print $fh $some_content;
close $fh;

On the other hand, adding something in the beginning (or in the middle) is very complicated.

另一方面,在开头(或中间)添加内容非常复杂。

One way to do it:

一种方法:

use File::Temp qw/ tempfile /;
use File::Copy;

open my $in, '<', 'some.file' or die "Cannot open file for reading: $!\n";

my ($out, $temp_file_name) = tempfile();

print $out $some_content;
my $l;
print $out $l while defined( $l = <$in> );

close $in;
close $out;

move( $temp_file_name, 'some.file' );

#2


This is covered by the FAQ

这包含在FAQ中

#3


The following code is the most simple example I can think of.

以下代码是我能想到的最简单的例子。

#!/usr/local/bin/perl
open (MYFILE, '>>data.txt');
print MYFILE "Bob\n";
close (MYFILE);

The '>>' tells the open function that you want to edit file by placing characters onto the end of it.

'>>'告诉open函数你想通过将字符放在它的末尾来编辑文件。

Replace this with '>' to replace the content instead.

将其替换为“>”以替换内容。

To place the content at the beginning of the file - I would suggesting reading in the file, manually placing the content at the begging of the char array, and then writing to the file with '>'

要将内容放在文件的开头 - 我建议在文件中读取,手动将内容放在char数组的乞讨处,然后用'>'写入文件

#4


Use the Tie::File module and it can be done easily although it may not be the most efficient way. You can edit a file just as if it is a perl array, so to add a line to the beginning you would use unshift() and add a line to the end you can use push(). The modules documentation is pretty clear on how to use it.

使用Tie :: File模块可以轻松完成,尽管它可能不是最有效的方法。您可以像编辑perl数组一样编辑文件,因此要在开头添加一行,您将使用unshift()并在末尾添加一行,您可以使用push()。模块文档非常清楚如何使用它。

#5


Are you looking for a templating system?

你在寻找模板系统吗?

#1


Appending is trivial:

追加是微不足道的:

open my $fh, '>>', 'some.file' or die "Cannot open file for append: $!\n";
print $fh $some_content;
close $fh;

On the other hand, adding something in the beginning (or in the middle) is very complicated.

另一方面,在开头(或中间)添加内容非常复杂。

One way to do it:

一种方法:

use File::Temp qw/ tempfile /;
use File::Copy;

open my $in, '<', 'some.file' or die "Cannot open file for reading: $!\n";

my ($out, $temp_file_name) = tempfile();

print $out $some_content;
my $l;
print $out $l while defined( $l = <$in> );

close $in;
close $out;

move( $temp_file_name, 'some.file' );

#2


This is covered by the FAQ

这包含在FAQ中

#3


The following code is the most simple example I can think of.

以下代码是我能想到的最简单的例子。

#!/usr/local/bin/perl
open (MYFILE, '>>data.txt');
print MYFILE "Bob\n";
close (MYFILE);

The '>>' tells the open function that you want to edit file by placing characters onto the end of it.

'>>'告诉open函数你想通过将字符放在它的末尾来编辑文件。

Replace this with '>' to replace the content instead.

将其替换为“>”以替换内容。

To place the content at the beginning of the file - I would suggesting reading in the file, manually placing the content at the begging of the char array, and then writing to the file with '>'

要将内容放在文件的开头 - 我建议在文件中读取,手动将内容放在char数组的乞讨处,然后用'>'写入文件

#4


Use the Tie::File module and it can be done easily although it may not be the most efficient way. You can edit a file just as if it is a perl array, so to add a line to the beginning you would use unshift() and add a line to the end you can use push(). The modules documentation is pretty clear on how to use it.

使用Tie :: File模块可以轻松完成,尽管它可能不是最有效的方法。您可以像编辑perl数组一样编辑文件,因此要在开头添加一行,您将使用unshift()并在末尾添加一行,您可以使用push()。模块文档非常清楚如何使用它。

#5


Are you looking for a templating system?

你在寻找模板系统吗?