开发者

Different ways of opening file in perl

开发者 https://www.devze.com 2023-03-03 13:58 出处:网络
I have seen that in perl sometimes to ope开发者_StackOverflow社区n a file for writing they use: open(my $file_handle, \">$file_name\");

I have seen that in perl sometimes to ope开发者_StackOverflow社区n 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 $!;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号