在Perl中打开和读取文件的最佳方式是什么? [英] What's the best way to open and read a file in Perl?
问题描述
我的一个非常常见的代码块Perl程序正在打开文件并读取或写入文件。我已经看到了这么多的方法,而且我执行这个任务的风格已经有了很多年的改变。我只是想知道最好的(如果有最好的方法)是这样做的?
我曾经打开一个文件如下所示:
my $ input_file =/ path / to / my / file
打开INPUT_FILE,&$ $ input_file||死无法打开$ input_file:$!\\\
;
但我认为有错误陷阱的问题。
添加括号似乎修复了错误陷阱:
open(INPUT_FILE,&$ $ input_file)| |死无法打开$ input_file:$!\\\
;
我知道你也可以为变量分配一个文件句柄,所以不要像我这样使用INPUT_FILE在上面,我可以使用$ input_filehandle - 这样更好吗?
对于读取文件,如果它很小,globbing有什么问题吗?
我的@array =< INPUT_FILE> ;;
或
my $ file_contents = join(\\\
,< INPUT_FILE>);
或者您应该永远循环,如下所示:
我的@array;
while(< INPUT_FILE>){
push(@array,$ _);
}
我知道有很多方法可以在perl中完成任务,只是想知道在文件中是否有首选/标准的开放阅读方法?
没有通用标准,但是是喜欢一个或另一个的理由。我的首选形式是:
打开(我的$ input_fh,<,$ input_file)死无法打开$ input_file:$!;
原因是:
- 您立即报告错误。 (如果您想要的话,将die替换为warn。)
- 您的文件句柄现在被引用计数,所以一旦你没有使用它,它将被自动关闭。如果您使用全局名称INPUT_FILEHANDLE,那么您必须手动关闭文件,否则将保持打开状态,直到程序退出。
- 读模式指示符<与$ input_file分离,增加可读性。
如果文件很小,你知道你想要所有行,以下是很好的:
我的@lines =< $ input_fh> ;;
如果您需要将所有行作为单个字符串处理,您甚至可以执行此操作:
我的$ text = join('',< $ input_fh>);
对于长文件,您将需要使用while来迭代行,或使用read。
Please note - I am not looking for the "right" way to open/read a file, or the way I should open/read a file every single time. I am just interested to find out what way most people use, and maybe learn a few new methods at the same time :)*
A very common block of code in my Perl programs is opening a file and reading or writing to it. I have seen so many ways of doing this, and my style on performing this task has changed over the years a few times. I'm just wondering what the best (if there is a best way) method is to do this?
I used to open a file like this:
my $input_file = "/path/to/my/file";
open INPUT_FILE, "<$input_file" || die "Can't open $input_file: $!\n";
But I think that has problems with error trapping.
Adding a parenthesis seems to fix the error trapping:
open (INPUT_FILE, "<$input_file") || die "Can't open $input_file: $!\n";
I know you can also assign a filehandle to a variable, so instead of using "INPUT_FILE" like I did above, I could have used $input_filehandle - is that way better?
For reading a file, if it is small, is there anything wrong with globbing, like this?
my @array = <INPUT_FILE>;
or
my $file_contents = join( "\n", <INPUT_FILE> );
or should you always loop through, like this:
my @array;
while (<INPUT_FILE>) {
push(@array, $_);
}
I know there are so many ways to accomplish things in perl, I'm just wondering if there are preferred/standard methods of opening and reading in a file?
There are no universal standards, but there are reasons to prefer one or another. My preferred form is this:
open( my $input_fh, "<", $input_file ) || die "Can't open $input_file: $!";
The reasons are:
- You report errors immediately. (Replace "die" with "warn" if that's what you want.)
- Your filehandle is now reference-counted, so once you're not using it it will be automatically closed. If you use the global name INPUT_FILEHANDLE, then you have to close the file manually or it will stay open until the program exits.
- The read-mode indicator "<" is separated from the $input_file, increasing readability.
The following is great if the file is small and you know you want all lines:
my @lines = <$input_fh>;
You can even do this, if you need to process all lines as a single string:
my $text = join('', <$input_fh>);
For long files you will want to iterate over lines with while, or use read.
这篇关于在Perl中打开和读取文件的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!