在perl中打开文件的不同方式 [英] Different ways of opening file in perl
问题描述
open(my $ file_handle, > $ FILE_NAME);
有时:
打开(FILE_HANDLE,> $ file_name);
有什么不同?
您应该查看 perlopentut(from Perl文档)和 perlfunc -f open(来自Perl POD)。这两个资源给你很多好的信息。当你在那里时,查看open的三个参数版本,以及错误检查。现在打开文件的一个很好的方法是:
pre $ 打开我的$ file_handle,'>',$ filename或者die $ !;
I have seen that in perl sometimes to open a file for writing they use:
open(my $file_handle, ">$file_name");
And sometimes:
open(FILE_HANDLE, ">$file_name");
What is the difference?
The first method you showed is the newer, and usually favorable method. It uses lexical filehandles (filehandles that are lexically scoped). The second method uses package-global typeglob filehandles. Their scoping is broader. Modern Perl programs usually use the 'my' version, unless they have a good reason not to.
You ought to have a look at perlopentut (from the Perl documentation), and perlfunc -f open (from the Perl POD). Those two resources give you a lot of good information. While you're there, look up the three argument version of open, as well as error checking. A really good way to open a file nowadays is:
open my $file_handle, '>', $filename or die $!;
这篇关于在perl中打开文件的不同方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!