在perl中打开文件的不同方式 [英] Different ways of opening file in perl

查看:122
本文介绍了在perl中打开文件的不同方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  open(my $ file_handle, > $ FILE_NAME); 

有时:

 打开(FILE_HANDLE,> $ file_name); 

有什么不同?

解决方案你展示的第一种方法是较新的,通常是有利的方法。它使用词法文件句柄(词法范围的文件句柄)。第二种方法使用包全局的typeglob文件句柄。他们的范围更广。现代Perl程序通常使用我的版本,除非他们有充分的理由不这样做。

您应该查看 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆