Path :: Class :: Unicode和错误处理

时间:2013-09-20 08:12:35

标签: perl file-io error-handling perl-module

Path::Class::Unicode:我是否必须自行检查ufileopen是否存在错误,还是自动完成?

use Path::Class::Unicode;

my $file = ufile( "filename" );
my $fh = $file->open;

1 个答案:

答案 0 :(得分:1)

ufile只是该类构造函数的一个wapper,它不应该抛出任何错误。

open方法是围绕IO::File(或IO::Dir的构造函数的wapper)。 new上的IO::File方法可能会为无效参数抛出错误,但否则会返回文件句柄或opensysopen的正常返回值等。但是,这由IO::File包装,以返回有效的文件句柄或undef

因此,您应该执行自己的错误处理:

my $fh = $file->open or die "Could not open $filename: $!";
# do not use the stringification of $file, as that would be a byte string

我不确定Path :: Class :: Unicode是否具有显着的优势,考虑到不同的操作系统如何处理unicode文件名。通常最好将文件名视为二进制数据,这是当前open内置函数的工作方式。